OneWay binding for ToggleButton's IsChecked property in WPF

后端 未结 3 1951
一向
一向 2021-02-15 08:05

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



        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-15 08:41

    I have similar problem.

    It is not "the binding seems to get lost" (unless it's earlier frameworks problems). Binding continues working and can be easily proved by changing property outside of that command (e.g. in handler of another button click event/command).

    The problem is what IsChecked can be changed in two ways: 1) binding (when value of SomeProperty is changed button will be updated 2) user (if user press button he will change IsChecked, but binding is OneWay so SomeProperty will not be updated).

    So you may have desync occurs when SomeProperty == false but button IsChecked == true or vice-versa.

    To optimize performance binding mechanism is checking if new value is different from current. So if desync occurs and you try to update SomeProperty with the value which it already have, then nothing will happens.

    Workaround is simple: update property in 2 steps

    SomeProperty = !set;
    SomeProperty = set;
    

    where set is the value you need (e.g. opposite to current SomeProperty).

提交回复
热议问题