Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneWay binding for ToggleButton's IsChecked property in WPF

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?

like image 933
sourcenouveau Avatar asked Sep 22 '10 16:09

sourcenouveau


1 Answers

To my knowledge Sinatr is correct regarding the 'desync' that occurs, at least in newer frameworks.

Another simple way around the issue is to remove the mode=oneway and implement an empty setter. Ex:

    bool _MyIsEnabled;
    public bool MyIsEnabled
    {
        get { return _MyIsEnabled; }
        set { }
    }

With this binding setup you can you change the value of the backing variable from your command binding function, or from whether you need to. Just remember to call RaisePropertyChanged.

like image 106
raistlin Avatar answered Nov 16 '22 00:11

raistlin