views:

76

answers:

2

I have a ToggleButton with its IsChecked property bound to a property using a OneWay binding.

<ToggleButton
    Command="{Binding Path=SomeCommand}"
    IsChecked="{Binding Path=SomeProperty, Mode=OneWay}" />

The SomeCommand toggles the boolean SomeProperty value, and a PropertyChanged event is raised for SomeProperty.

If I change SomeProperty in my viewmodel the ToggleButton depresses correctly. However if I click the ToggleButton the binding seems to get lost and the button no longer gets checked according to the value of SomeProperty. Any ideas on how to fix this problem?

+2  A: 

This is by design when using oneway data binding. Add the attached property PresentationTraceSources.TraceLevel=High to your binding and you will see when it gets disconnected. This link describes the problem as well (without offering any solution): Debugging Data Binding in WPF

The way I normally solve it is to use a command for user interaction and code behind to change the control appearance because of some changed properties.

Wallstreet Programmer
A: 

I think what the problem boils down to is a conflict between the command and the IsChecked binding. My solution was to change my viewmodel to expose a bool? and bind that to the IsChecked property. I did not bind a command to the ToggleButton. Elsewhere in my code where I want to toggle the property I use SomeCommand.

emddudley