Home > DeveloperSection > Forums > Checkbox isEnabled behaviour unable to figure out
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    September-23-2013 7:46 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 940  View(s)
Rate this:

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.

<Grid>

    <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" />

</Grid>

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;

        }

        else

        {

            _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?



Pravesh Singh

Total Post:411

Points:2881
Posted on    September-23-2013 8:17 AM

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();

window.Show();


Don't want to miss updates? Please click the below button!

Follow MindStick