将一个只读依赖属性的数据绑定到Xaml中的ViewModel [英] Databind a read only dependency property to ViewModel in Xaml
问题描述
我正在将视图模型中的Button的 IsMouseOver
只读依赖属性数据绑定到布尔的读/写属性。 / p>
基本上我需要将Button的 IsMouseOver
属性值读取到视图模型的属性。
< Button IsMouseOver ={Binding Path = IsMouseOverProperty,Mode = OneWayToSource}/>
我收到一个编译错误:'IsMouseOver'属性是只读的,不能
没有错误。这是一个 WPF的限制 - 只读属性无法绑定 OneWayToSource
除非源也是 DependencyProperty
。
另一种替代方法是附加的行为。
I'm trying to databind a Button's IsMouseOver
read-only dependency property to a boolean read/write property in my view model.
Basically I need the Button's IsMouseOver
property value to be read to a view model's property.
<Button IsMouseOver="{Binding Path=IsMouseOverProperty, Mode=OneWayToSource}" />
I'm getting a compile error: 'IsMouseOver' property is read-only and cannot be set from markup. What am I doing wrong?
No mistake. This is a limitation of WPF - a read-only property cannot be bound OneWayToSource
unless the source is also a DependencyProperty
.
An alternative is an attached behavior.
这篇关于将一个只读依赖属性的数据绑定到Xaml中的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!