简体   繁体   English

如何将两个东西绑定到复选框的 IsChecked 属性?

[英]How to bind two things to IsChecked property of checkbox?

Ok, so I have a datagrid with a checkbox in a DataGridTemplateColumn since I would like to do a single-click check.好的,所以我在 DataGridTemplateColumn 中有一个带有复选框的数据网格,因为我想进行单击检查。 I also have a "select all" checkbox at the top.我在顶部还有一个“全选”复选框。 (I disabled the header because I wanted to make a custom one). (我禁用了 header 因为我想定制一个)。 My datagrid is programmatically populated.我的数据网格是以编程方式填充的。 and I'm using the mahApps nuget package (if that means anything).我正在使用 mahApps nuget package (如果这意味着什么)。

What I want to do is bind the "Select All" status to the IsChecked property on my populated checkboxes but also be able to click the row to check the checkbox.我想要做的是将“全选”状态绑定到我填充的复选框上的 IsChecked 属性,但也能够单击该行以选中该复选框。 But to be able to do that, I need to add: IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}" .但为了能够做到这一点,我需要添加: IsChecked="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}"

I have both of them, and they both work, but I can only seem to use one at a time.我有他们两个,他们都工作,但我似乎一次只能使用一个。 How would I go about doing both?我将如何 go 两者都做? Please let me know if you need more info如果您需要更多信息,请告诉我

Edit: I would also need multi-selection!编辑:我还需要多选!

This is psuedo code:这是伪代码:

To make each rows checkbox clickable independently - the ViewModel for row should have bool IsChecked get set property.要使每行复选框可独立点击 - 行的 ViewModel 应该具有 bool IsChecked 获取设置属性。 For Top level "Select All" checkbox - you will need a top level bool?对于顶级“全选”复选框 - 您需要顶级布尔值吗? SelectAll get set property too - getter will be something like -- SelectAll 也获得设置属性 - getter 将类似于 --

Lets says collection bound to GridView is让我们说绑定到 GridView 的集合是

List<RowViewModel>RowViewModelCollection;

Then each RowViewModel object will have:然后每个 RowViewModel object 将具有:

private bool _IsSelected = null;
public bool IsSelected
{
    get => _IsSelected;
    set
    {
        _IsSelected  = value;
        // NotifyProperty Change for this property
        // Notify Property Change for TopLevel SelectAll property
    }
}

Your Top level SelectAll should look like:您的顶级 SelectAll 应如下所示:

public bool? SelectAll
{
    get
    {
        if(RowViewModelCollection.All(r=>r.IsSelected == true))
            return true;
        else if(RowViewModelCollection.All(r=>r.IsSelected == false))
            return false;
        else
            return null;
    }
    set
    {
       foreach(var row in RowViewModelCollection)
       {
          row.IsSelected = value;
       }

       // Notify Property Changed for This property
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WPF中,如何将Checkbox的IsChecked绑定到List &lt;&gt;。Contains? - In WPF, how do I two-way bind a Checkbox's IsChecked to property to List<>.Contains? 如何将 WPF 复选框的 IsChecked 属性绑定到非窗口对象的布尔属性 - How can I bind a WPF checkbox's IsChecked property to a boolean property of an object that is not a window 如何将CheckBox IsChecked绑定到CheckBoxes的ListBox - How to bind CheckBox IsChecked to ListBox of CheckBoxes 如何为我的bool属性正确实现INotifyPropertyChanged并绑定到CheckBox.IsChecked? - How do I correctly implement INotifyPropertyChanged for my bool property and bind to CheckBox.IsChecked? 如何绑定菜单项的IsChecked属性 - How to Bind IsChecked Property of Menu item 在列表框中获取复选框的IsChecked属性 - Getting IsChecked Property of a CheckBox in a ListBox 如何获取数据模板中Checkbox的isChecked属性的值 - How to get the value of isChecked property of a Checkbox in a data template 如何将布尔值列表分配给 WPF 中生成的 Checkbox IsChecked 属性? - How to assign a List of Booleans to a generated Checkbox IsChecked property in WPF? 如何动画IsChecked属性? - How to animate IsChecked property? 将CheckBox IsChecked属性绑定到ListView的SelectedItems属性 - Binding the CheckBox IsChecked Property to SelectedItems Property of a ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM