1
  <CheckBox x:Name="cb1" IsChecked="{Binding cb1Enabled}"/>
  <CheckBox x:Name="cb2" IsEnabled="{Binding ElementName=cb1, Path=IsChecked}" IsChecked="{Binding cb2Enabled}">

I have this two CheckBoxes cb1 and cb2. Both are bound to 2 bools. cb1Enabled and cb2Enabled. As you can see, cb2 is enabled only when cb1 is checked and also it's IsChecked property is bound to the specific bool dependency property.

It's all well and fine but i'd like to uncheck cb2 when cb1 is unchecked and so, set the cb2Enabled to false. Is there a way to accomplish that?

So i think i can say that i want cb2's IsChecked property to be bound to the IsChecked property of cb1 and also to the cb2Enabled dp.

Olaru Mircea
  • 2,570
  • 26
  • 49
  • Re: your addition: I don't think you can really say that "cb2's IsChecked property to be bound to the IsChecked property of cb1", because you don't want checking cb1 to cause cb2 to become checked ... right? Rather, you want a trigger when cb1 is *unchecked*. – McGarnagle May 06 '14 at 21:31

1 Answers1

1

This is probably best done in the view model. Use a two-way binding:

<CheckBox x:Name="cb1" IsChecked="{Binding cb1Enabled,Mode=TwoWay}"/>

Then, set the "cb2Enabled" property in the setter for "cb1Enabled". Eg:

public bool cb1Enabled
{
    get { return bool _cb1Enabled; }
    set
    {
        _cb1Enabled = value;
        if (!_cb1Enabled)
            cb2Enabled = false;
        RaisePropertyChanged();
    }
}
private bool _cb1Enabled;

If you don't like the above, a XAML-only alternative is to use an event trigger on Unchecked with ChangePropertyAction:

<CheckBox x:Name="cb1" IsChecked="{Binding cb1Enabled}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Unchecked">
            <ei:ChangePropertyAction TargetObject="{Binding}" PropertyName="cb2Enabled" 
                                     Value="False" 
                                     />
        </i:EventTrigger EventName="Unchecked">
    </i:Interaction.Triggers>
</CheckBox>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260