【问题标题】:Unchecking One of the child checkboxes should uncheck the parent checkbox in WPF MVVM pattern取消选中其中一个子复选框应取消选中 WPF MVVM 模式中的父复选框
【发布时间】:2023-03-31 21:00:01
【问题描述】:

前两种方法对我有用,我下面提到的第三和第四点不起作用

  1. 如果我选中标题复选框,所有子复选框都会被选中
  2. 如果取消选中,则全部取消选中
  3. 如果所有内容都被选中..如果我取消选中一个子复选框...标题复选框将被取消选中
  4. 如果我一一检查所有子复选框....标题复选框将被选中。

这是我的代码

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <CheckBox Content="Included" x:Name="headerCheckBox" />
    </DataGridTemplateColumn.Header>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Name="chkselectAll" IsChecked="{Binding IsChecked,ElementName=headerCheckBox,Mode=OneWay}"></CheckBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

我能在这里得到一些帮助吗?如何在 xaml 中实现它?

我想要这样的全选复选框行为

https://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat#

【问题讨论】:

标签: c# wpf xaml checkbox mvvm


【解决方案1】:

您应该将CellTemplate 中的CheckBox 绑定到数据对象的源属性:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox Name="chkselectAll" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

确保数据对象实现INotifyPropertyChanged接口:

public class Item : INotifyPropertyChanged
{
    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后,您可以将标头 CheckBox 的属性绑定到您的视图模型的属性,您可以在 DataGrid 的源集合中的项目更改时设置该属性,例如:

<DataGridTemplateColumn.Header>
    <CheckBox Content="Included"
                x:Name="headerCheckBox"
                IsChecked="{Binding DataContext.AllChecked, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataGridTemplateColumn.Header>

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        Items = new ObservableCollection<Item>();
        Items.CollectionChanged += Items_CollectionChanged;

        //add the items..:
        Items.Add(new Item());
        Items.Add(new Item() { IsChecked = true });
        Items.Add(new Item());
    }

    private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (object item in e.NewItems)
            {
                (item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }

        if (e.OldItems != null)
        {
            foreach (object country in e.OldItems)
            {
                (country as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnPropertyChanged("AllChecked");
    }

    public ObservableCollection<Item> Items { get; private set; }

    public bool AllChecked
    {
        get { return Items.All(x => x.IsChecked); }
        set
        {
            foreach (var item in Items)
                item.IsChecked = value;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

【讨论】:

  • public ProgramViewModel(SFProgram program, bool hasBeenCreated) : base(null, true) { Items = new ObservableCollection(); Items.CollectionChanged += Items_CollectionChanged; //添加项目..: Items.Add(new Item()); Items.Add(new Item() { IsChecked = true });项目。添加(新项目()); _program = 程序;
  • 你能告诉我如何添加项目,而且我的构造函数是参数化的,可以吗?当我们使用 mvvm 模式时,您能否在 xaml 或 viewmodel.cs 中为我提供任何更简单的解决方案
  • 我的示例显然是 MVVM...您不知道如何将项目添加到集合或您在问什么?
  • 这一行我不明白 Items.Add(new Item() { IsChecked = true });
  • 它将默认选中的新项目添加到源集合中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
相关资源
最近更新 更多