使用绑定的DataTrigger条件的Value属性绑定、属性、条件、DataTrigger

2023-09-03 02:29:21 作者:/

我工作的一个WPF应用程序和数据的触发挣扎。我想的触发条件的值绑定到某个对象,我有:

 < D​​ataTrigger绑定={结合美孚}
             值={结合的ElementName = AnotherElement,路径=酒吧}> ..
 

但是,我不能,因为它似乎并不可能使用Value属性绑定。是吗?我能做到这一点不知?我得到以下错误:

  

一个'绑定'不能在类型为DataTrigger的值属性设置。 A'绑定'只能在DependencyObject的一个DependencyProperty的设置。

解决方案 C WebForm中的DropDownList控件的SeletedItem属性怎么转化为其绑定的数据对象

没有,那是不可能的。由于错误消息指出,只有依赖属性可以是WPF绑定的目标,DataTrigger.Value不是一个依赖项属性。所以,你将需要分配一个实际值。

解决方法是使用一个MultiBinding其子绑定,你要比较的两个绑定,与IMultiValueConverter如果两个输入是平等的,假的,如果他们不相等返回true。然后,DataTrigger可以使用该MultiBinding,和值true。

I'm working on a WPF application and struggling with a data trigger. I'd like to bind the value of the trigger condition to some object I have:

<DataTrigger Binding="{Binding Foo}" 
             Value="{Binding ElementName=AnotherElement, Path=Bar}">..

However, I'm not allowed as it doesn't seem to be possible to use bindings for the Value property. Is it? Can I achieve this somehow? I get the following error:

A 'Binding' cannot be set on the 'Value' property of type 'DataTrigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

解决方案

No, it is not possible. As the error message says, only dependency properties can be targets of WPF bindings, and DataTrigger.Value is not a dependency property. So you will need to assign an actual value.

The workaround is to use a MultiBinding whose child Bindings are the two bindings you want to compare, with an IMultiValueConverter which returns true if the two inputs are equal and false if they are unequal. The DataTrigger can then use that MultiBinding, and a Value of True.