当复选框被禁用时,您无法更改值。
要在 MVVM 中执行此操作,您必须在禁用主复选框之前更改值:
C#
/// <summary>
/// Bind to IsChecked of "ControlchkEnable" element (TwoWay)
/// and bind to IsEnabled of each of other 7 checkbox's (OneWay)
/// </summary>
public bool ControlchkEnable
{
get { return _controlchkEnable; }
set
{
if (value == _controlchkEnable) return;
_controlchkEnable = value;
// Before informing the checkboxes are disabled,
// pass their values to uncheck
if (!_controlchkEnable)
{
Check1 = false;
// Check2 = false;
// Check...= false;
}
// Raise UI that value changed
RaisePropertyChanged("ControlchkEnable");
}
}
private bool _controlchkEnable;
/// <summary>
/// Bind to IsChecked of one of other 7 checkbox's (TwoWay)
/// </summary>
public bool Check1
{
get { return _check1; }
set
{
if (value == _check1) return;
_check1 = value;
RaisePropertyChanged("Check1");
}
}
private bool _check1;
xml:
<!-- Main checkbox -->
IsChecked="{Binding ControlchkEnable, Mode=TwoWay}"
<!-- Other checkbox's -->
IsEnabled="{Binding ControlchkEnable, Mode=OneWay}"
IsChecked="{Binding Check1, Mode=TwoWay}"