WPF中ToggleButton的IsChecked属性的One

WPF中ToggleButton的IsChecked属性的One

本文介绍了WPF中ToggleButton的IsChecked属性的OneWay绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ToggleButton ,它的 IsChecked 属性使用OneWay绑定绑定到了一个属性。

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

SomeCommand 切换布尔值 SomeProperty 值,并为 SomeProperty 引发 PropertyChanged 事件。

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

如果我在视图模型中更改了 SomeProperty ,则 ToggleButton 会正确按下。但是,如果单击 ToggleButton ,绑定似乎会丢失,并且按钮将不再根据 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?

推荐答案

这是设计使用单向数据绑定时的。将附加的属性PresentationTraceSources.TraceLevel = High添加到绑定中,您将看到断开连接的时间。此链接也描述了问题(不提供任何解决方案):

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.

这篇关于WPF中ToggleButton的IsChecked属性的OneWay绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:44