これが私がそれを行う方法です:
Select AllCheckboxとバインドされたプロパティは次のとおりです。
<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!!
}
Bindingfor yourCheckBoxがTwoWayモードになり、 with であることに注意してくださいUpdateSourceTrigger=PropertyChanged。これにより、次のことが可能になります。
- バインドされたプロパティの値をコードから変更する (
TwoWay)
CheckBox値が更新されたときに状態を更新する( UpdateSourceTrigger=PropertyChanged)
次: チェックボックスの 1 つと、コード内の同等のものを次に示します。
<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!!
}
そして、これはトリックを行う必要があります: 1 つboolが更新されると、selectAllbool も更新され、その逆も同様です。