gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:29 25 4
gpt4 key购买 nike

在 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 相关联(这很可能在 WindowUserControl 代码中后面,尽管有很多方法可以实现这一点)。

最后,更新您的绑定(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 中运行的代码。

关于c# - 如何处理 MVVM 模式中的多个复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7557209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com