Sujoy
Sujoy

Reputation: 57

silverlight : Checkbox checked/unchecked with another checkbox checked/unchecked

I have few checkbox's (8 of them) one of which is for Enable/Disable other 7 checkbox's.

for that I have written as,

IsEnabled="{Binding ElementName=ControlchkEnable, Path=IsChecked, Mode=OneWay}"
IsChecked="{Binding ElementName=ControlchkEnable, Path=IsChecked, Mode=OneWay}"

in every dependent CB's. Now enable/Disable is working fine, but if Master checkbox is unchecked, then other checkbox's are not getting unchecked, they are just getting disabled.

Any idea what went wrong?

Upvotes: 1

Views: 1583

Answers (1)

Tonio
Tonio

Reputation: 743

When the checkbox is disabled you cannot change value.

To do that in MVVM, you have to change values before disabled the main checkbox :

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;

Xaml :

<!-- Main checkbox -->
IsChecked="{Binding ControlchkEnable, Mode=TwoWay}"

<!-- Other checkbox's -->
IsEnabled="{Binding ControlchkEnable, Mode=OneWay}"
IsChecked="{Binding Check1, Mode=TwoWay}"

Upvotes: 1

Related Questions