Skip to main content
 首页 » 编程设计

c#之如何处理 MVVM 模式中的多个复选框

2024年02月23日16fff_TT

在 WPF 中绑定(bind)复选框是一个常见问题,但我仍然没有找到适合初学者的示例代码。我在 WPF 中有复选框列表来选择最喜欢的运动名称。在我的例子中,复选框的数量是静态的。谁能告诉我如何针对这个问题实现 ViewModel?

FavoriteSportsView.xaml:

  <StackPanel Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"  
  Width="150"> 
 
  <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"   
  Command="{Binding Path=SportsResponseCommand}"  
  CommandParameter="Football"  
  Content="Football"  
  Margin="5" /> 
 
  <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"  
  Command="{Binding Path=SportsResponseCommand}"  
  CommandParameter="Hockey"  
  Content="Hockey"  
  Margin="5" /> 
 
  <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"  
  Command="{Binding Path=SportsResponseCommand}"  
  CommandParameter="Golf"  
  Content="Golf"  
  Margin="5" /> 
  </StackPanel> 

FavoriteSportsViewModel.cs

  public class FavoriteSportsViewModel.cs { 
 
    //Since I am using the same IsChecked in all check box options, I found all check  
    //boxes gets either checked or unchecked when I just check or uncheck one option. 
    //How do i resolve this issue? I don't think i need seprate IsChecked for each  
    //check box option. 
 
    private bool _isChecked; 
    public bool IsChecked{ 
      get { 
           return _isChecked; 
       } 
 
      set { if (value != _isChecked)  
             _isChecked = value; 
            this.OnPropertyChanged("IsChecked"); 
       } 
    } 
 
 
    //How do i detect parameter in this method? 
    private ICommand _sportsResponseCommand; 
    public ICommand SportsResponseCommand 
    { 
        get 
        { 
            if (_sportsResponseCommand== null) 
                _sportsResponseCommand= new 
                    RelayCommand(a => DoCollectSelectedGames(), p => true); 
            return _sportsResponseCommand; 
        } 
        set 
        { 
            _sportsResponseCommand= value; 
 
        } 
 
    } 
 
    private void DoCollectSelectedGames(){  
      //Here i push all selected games in an array 
    } 
 
 
    public abstract class ViewModelBase : INotifyPropertyChanged 
    { 
       public event PropertyChangedEventHandler PropertyChanged; 
       public void OnPropertyChanged(string propertyName) 
       { 
         if (PropertyChanged != null) 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
       } 
    } 
 
  } 

我不确定如何在上面的 ViewModel 中执行以下操作: 1. 如何实现单一方法来处理我的所有选项? 2.如何检测每个复选框以查看是否选中 3. 如何使用 CommandParameter? 4. 如何正确实现 SportsResponseCommand

请您参考如下方法:

您的 View 模型应如下所示:

public class MyViewModel : INotifyPropertyChanged 
{ 
    //INotifyPropertyChanged implementation 
    public event PropertyChangedEventHandler PropertyChanged; 
 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
        if (this.PropertyChanged != null) 
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
 
    //bindable property 
    private bool _football; 
    public bool Football 
    { 
        get { return _football; } 
        set 
        { 
            if (value != _football) 
            { 
                _football = value; 
                this.OnPropertyChanged("Football"); 
            } 
        } 
    } 
 
    //... and the same for Golf and Hockey 
} 

然后通过设置 DataContext 属性将 View 模型与 View 相关联(这很可能在 Window 或 UserControl 代码中后面,尽管有很多方法可以实现这一点)。

最后,更新您的绑定(bind),使其看起来像:

<CheckBox IsChecked="{Binding Football, Mode=TwoWay}"   
 Content="Football"  
 Margin="5" /> 
 
<CheckBox IsChecked="{Binding Golf, Mode=TwoWay}"    
 Content="Football"  
 Margin="5" /> 

作为最后的评论,您实际上不需要绑定(bind) Command 属性 - 您可以编写任何需要在 View 模型的属性 setter 中运行的代码。