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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:18:28  来源:igfitidea点击:

WPF: Check/Uncheck all checkbox for checkboxes located in gridview cell template?

c#wpfdata-bindinggridviewcheckbox

提问by Robin

I'm trying to create a check/uncheck all CheckBoxfor a number of CheckBoxesthat are located inside the cell template of a GridViewColumn. I added this column to a GridView(along with other columns), set the GridViewto the view property of a ListView, and then databound the ListViewto a collection of custom DataObjects. So, each row of the ListViewhas 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 CheckBoxby binding the IsCheckedproperty of the CheckBoxes, but I do not want to change the data object the ListViewis bound to. My first attempt was to bind the ListView to a Dictionary<DataObject,Boolean>and then bind the IsCheckedproperty to the Valueof the Dictionaryand the other columns to Key.DataObjectProperty. Then, I simply toggled the Valuesof the Dictionary when then check/uncheck all CheckBoxwas clicked. The binding to worked properly, but apparently dictionaries don't support change notification so the CheckBoxeswere never updated.

我想CheckBox通过绑定 的IsChecked属性来创建所有选中/取消选中CheckBoxes,但我不想更改ListView绑定到的数据对象。我的第一次尝试是将 ListView 绑定到 a Dictionary<DataObject,Boolean>,然后将IsChecked属性绑定到ValueDictionary和其他列到KeyDataObjectProperty. 然后,ValuesCheckBox单击选中/取消选中所有内容时,我只是切换了字典的。绑定正常工作,但显然字典不支持更改通知,因此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));
    }
}