[英]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.