Advertise with us

Checkbox isEnabled behaviour unable to figure out

Hi guys

I have three CheckBox in my WPF application and what I want is to disable/ Enable the CheckBoxes according to a Property in ViewModel.

    <CheckBox x:Name="cbTest1" Margin="12,341,476,5" IsEnabled="{Binding Path=BoolProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    <CheckBox x:Name="cbTest3" Margin="74,341,414,5" IsEnabled="{Binding Path=BoolProperty, Mode=TwoWay }" />
    <CheckBox x:Name="cbTest2" Margin="131,341,359,5" IsEnabled="{Binding Path=BoolProperty, Mode=TwoWay }" />
    <Button x:Name="btnClick" Click="btnClick_Click" Margin="231,333,29,5" />
In my testing application I have written a code behind for button click
private void btnClick_Click(object sender, RoutedEventArgs e)
        if (_boolProperty == true)
            _boolProperty = false;
            cbTest1.IsEnabled = false;
            _boolProperty = true;
            cbTest1.IsEnabled = true;

The weird behavior is that if I comment the cbTest1.IsEnabled = false; or cbTest1.IsEnabled = true; then the CheckBox es remain as disabled,

but changes in only one CheckBox say, "cbTest1"'s value to true also make changes in the other checkBoxes as well. Like if cbTest1.IsEnabled = true; then all three CheckBox are enabled. if cbTest1.IsEnabled = false; then all three CheckBox are disabled.

Anybody can enlighten me please. Why is this happening? How does binding happening here? One more question, how do I go for binding of the checkBoxes isEnabled Property with a property in viewModel?

Last updated:9/23/2013 8:17:12 AM

1 Answers

Pravesh Singh
Pravesh Singh

Hi Royce,

<CheckBox x:Name="cbTest1" IsEnabled="{Binding Path=BoolProperty, Mode=TwoWay}" />

<CheckBox x:Name="cbTest3" IsEnabled="{Binding Path=BoolProperty, Mode=TwoWay}" />

<CheckBox x:Name="cbTest2" IsEnabled="{Binding Path=BoolProperty, Mode=TwoWay}" />

All three check boxes bind to the same source, so no matter what you do, they will always share the same state. Furthermore, your binding is a two-way binding, so setting IsEnabled manually for one checkbox will make the underlying property you bound to change too which will then again trigger the other two checkboxes to update.

If you want to have separate states, you will have to use three different properties to bind to.

how do I go for binding of the checkBoxes isEnabled Property with a property in viewModel?

What you did is already correct (minus the shared property). So if you bind to BoolProperty1, BoolProperty2 and BoolProperty3 and each of those properties exists in your view model, then everything is good to go. All you need to make sure then is that the checkboxes have the view model as their data context. This is usually achieved by giving a parent component (often the Window itself) the data context:

Window window = new MyWindow();

window.DataContext = new MyViewModel();


Last updated:9/23/2013 8:17:12 AM