wpf 如何使用 MVVM 获取选中的复选框值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24215649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the checked checkbox values using MVVM?
提问by Euridice01
Right now, I'm working on a WPF project.
现在,我正在做一个 WPF 项目。
I have a confirmation dialog that displays a list of repeated strings that the user imported from a CSV file.
我有一个确认对话框,显示用户从 CSV 文件导入的重复字符串列表。
The user can check off the repeated strings they want to add to a list (listview) and click ok.
用户可以勾选他们想要添加到列表 (listview) 中的重复字符串,然后单击确定。
The problem is I'm not sure how to get the value of the checkbox items that are checked off and add them to a list. How do I know what checkbox is selected and add the selected items to a list?
问题是我不确定如何获取被选中的复选框项的值并将它们添加到列表中。我如何知道选中了哪个复选框并将所选项目添加到列表中?
I want to do this task using MVVM and not code behind. I've seen a few examples using code behind and they still confuse me.
我想使用 MVVM 而不是代码来完成这项任务。我已经看到一些使用隐藏代码的例子,但它们仍然让我感到困惑。
I think I have to use commands but I'm not sure still how to get the selected checkbox value?
我想我必须使用命令,但我仍然不确定如何获取选定的复选框值?
Here's my attempt:
这是我的尝试:
Code in ViewModel:
视图模型中的代码:
private bool isSelected;
public bool IsSelected
{
get
{
return isSelected;
}
set
{
isSelected = value;
RaisePropertyChanged("IsSelected");
if (IsSelected == true)
{
foreach (var item in confirmationList.PassList)
{
List.Add(item);
}
RaisePropertyChanged(LIST);
}
else if (IsSelected == false)
{
}
}
}
XAML:
XAML:
<Grid>
<ListView ItemsSource="{Binding Inventory}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}"
Header="Name">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Not sure what else to do from here to get the checked off values?
不知道从这里还可以做什么来获得检查的值?
EDIT: updated xaml to reflect look.
编辑:更新 xaml 以反映外观。
回答by pushpraj
Start by defining a model class
首先定义一个模型类
public class ListModel
{
public string Data { get; set; }
public bool IsSelected { get; set; }
}
Then Bind the list of this model to the Listview's ItemsSource
然后将这个model的list绑定到Listview的ItemsSource
List<ListModel> data = new List<ListModel>();
modify the binding of your check box to
将复选框的绑定修改为
<CheckBox Content="{Binding Data}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay"/>
then when you need to retrieve you may execute the following linq to get all selected strings
然后当您需要检索时,您可以执行以下 linq 以获取所有选定的字符串
IEnumerable<String> selectedData = data.Where(d => d.IsSelected).Select(d => d.Data);
now you'll have all the data which is selected in the UI in the field selectedData
现在,您将拥有在 UI 中的 selectedData 字段中选择的所有数据
回答by CampDev
A excellent way is create a new class, because when you must bind the properties into Grid, ItemsControl or other element that it use ItemsSource is easier.
一个很好的方法是创建一个新类,因为当您必须将属性绑定到 Grid、ItemsControl 或其他元素时,它使用 ItemsSource 会更容易。
In your model, the class will be...
在您的模型中,类将是...
public class ListModel: INotifyPropertyChanged
{
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
if (isSelected != value)
{
isSelected = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
}
}
}
You binding this property to CheckBox
您将此属性绑定到 CheckBox
<ItemsControl ItemsSource="{Binding ListModelBinding, Mode=TwoWay}">
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</ItemsControl>
In your ViewModel
在您的视图模型中
private ObservableCollection<ListModel> _listModelBinding;
public ObservableCollection<ListModel> ListModelBinding
{
get { return _listModelBinding; }
set { _listModelBinding= value; RaisePropertyChanged("ListModelBinding"); }
}
The solution serve several items. If you only one property, simply create a bool property like I created ListModelBinding in ViewModel, and after you bind this property to CheckBox in xaml.
该解决方案服务于几个项目。如果您只有一个属性,只需像我在 ViewModel 中创建 ListModelBinding 一样创建一个 bool 属性,然后将此属性绑定到 xaml 中的 CheckBox。
When you want retrieve data only apply Linq in ListModelBinding and that's it.
当您想检索数据时,仅在 ListModelBinding 中应用 Linq,仅此而已。

