C# WPF:选中/取消选中位于 gridview 单元格模板中的复选框的所有复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/326374/
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
WPF: Check/Uncheck all checkbox for checkboxes located in gridview cell template?
提问by Robin
I'm trying to create a check/uncheck all CheckBox
for a number of CheckBoxes
that are located inside the cell template of a GridViewColumn
. I added this column to a GridView
(along with other columns), set the GridView
to the view property of a ListView
, and then databound the ListView
to a collection of custom DataObjects
. So, each row of the ListView
has a column that contains a checkbox as well as columns bound to property paths of the bound object.
我试图创建一个检查/取消所有CheckBox
为一些CheckBoxes
在位于的小区模板中GridViewColumn
。我将此列添加到 a GridView
(以及其他列),将 设置GridView
为 a 的视图属性ListView
,然后将 数据绑定ListView
到 custom 的集合DataObjects
。因此, 的每一行ListView
都有一个包含复选框的列以及绑定到绑定对象的属性路径的列。
I would like to create the check/uncheck all CheckBox
by binding the IsChecked
property of the CheckBoxes
, but I do not want to change the data object the ListView
is bound to. My first attempt was to bind the ListView to a Dictionary<DataObject,Boolean>
and then bind the IsChecked
property to the Value
of the Dictionary
and the other columns to Key
.DataObjectProperty
. Then, I simply toggled the Values
of the Dictionary when then check/uncheck all CheckBox
was clicked. The binding to worked properly, but apparently dictionaries don't support change notification so the CheckBoxes
were never updated.
我想CheckBox
通过绑定 的IsChecked
属性来创建所有选中/取消选中CheckBoxes
,但我不想更改ListView
绑定到的数据对象。我的第一次尝试是将 ListView 绑定到 a Dictionary<DataObject,Boolean>
,然后将IsChecked
属性绑定到Value
的Dictionary
和其他列到Key
。DataObjectProperty
. 然后,Values
当CheckBox
单击选中/取消选中所有内容时,我只是切换了字典的。绑定正常工作,但显然字典不支持更改通知,因此CheckBoxes
从未更新。
Does anyone have any suggestions as to the best way to solve this problem?
有没有人对解决此问题的最佳方法有任何建议?
采纳答案by Jobi Joy
The only way I can think is to wrap your DataObject and boolean inside a new class which implements INotofyPropertyChanged. say the new class is YourCollection. Bind an ObservableCollection< YourNewClass >
instance to your ListView
我能想到的唯一方法是将您的 DataObject 和布尔值包装在一个实现 INotofyPropertyChanged 的新类中。说新类是 YourCollection。将ObservableCollection< YourNewClass >
实例绑定到您的 ListView
public class YourNewClass :INotifyPropertyChanged
{
public YourDataObject Object { get; set; }
private bool _isChecked;
public bool IsChecked
{
get
{
return _isChecked;
}
set
{
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}