WPF:选中/取消选中位于gridview单元格模板中的复选框的所有复选框? [英] WPF: Check/Uncheck all checkbox for checkboxes located in gridview cell template?

查看:392
本文介绍了WPF:选中/取消选中位于gridview单元格模板中的复选框的所有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为位于内部的复选框创建一个检查/取消选中所有复选框 GridViewColumn 的单元格模板。我将这个列添加到 GridView (与其他列一起),将 GridView 设置为 ListView ,然后将 ListView 数据绑定到自定义 DataObjects 。因此, ListView 的每一行都有一个列,其中包含一个复选框以及绑定到绑定对象的属性路径的列。

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.

我想通过绑定 IsChecked 属性创建检查/取消选中所有 CheckBox code> CheckBoxes ,但我不想更改 ListView 绑定的数据对象。我的第一个尝试是将ListView绑定到字典< DataObject,Boolean> ,然后将 IsChecked 属性绑定到字典 Value 以及的其他列。 DataObjectProperty 。然后,我简单地切换了字典的 Values ,然后选中/取消选中所有 CheckBox 。绑定工作正常,但显然字典不支持更改通知,因此 CheckBoxes 从未更新。

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.

有没有人对解决这个问题的最好方法有任何建议?

Does anyone have any suggestions as to the best way to solve this problem?

推荐答案

DataObject和boolean在一个新类中实现INotofyPropertyChanged。说新类是YourCollection。绑定 ObservableCollection< YourNewClass> 实例到您的ListView

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

   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));
    }
}

这篇关于WPF:选中/取消选中位于gridview单元格模板中的复选框的所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆