这是我的做法:
这是您的全选Checkbox
和绑定属性:
<CheckBox Margin="20,15,0,15" Content="Select all"
IsChecked="{Binding Path=IsSelectAllChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
public bool IsSelectAllChecked
{
get
{
return isSelectAllChecked
}
set
{
isSelectAllChecked = value;
// Do some code here to turn all selected booleans to true
SelectAll();
OnPropertyChanged("IsSelectAllChecked"); // Fire OnPropertyChanged event, important for TwoWay Binding!!
}
请注意,Binding
for yourCheckBox
现在处于TwoWay
模式,并且带有UpdateSourceTrigger=PropertyChanged
. 这将允许:
- 从代码 (
TwoWay
)更改绑定属性的值
- 更新
CheckBox
值时更新状态 ( UpdateSourceTrigger=PropertyChanged
)
接下来:这是您的复选框之一,代码中的等价物
<CheckBox Content="Select one"
IsChecked="{Binding Path=OneOfYourBools, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
public bool OneOfYourBools
{
get
{
return oneOfYourBools;
}
set
{
oneOfYourBools = value;
// If the isAllSelected was true, turn it to false!
if (this.IsSelectAllChecked)
{
this.IsSelectAllChecked = false;
}
OnPropertyChanged("OneOfYourBools"); // Fire OnPropertyChanged event, important for TwoWay Binding!!
}
这应该可以解决问题:当bool
更新一个时,selectAll
布尔值也会更新,反之亦然