对 DataTrigger 条件的 Value 属性使用绑定 [英] Using binding for the Value property of DataTrigger condition

查看:23
本文介绍了对 DataTrigger 条件的 Value 属性使用绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 WPF 应用程序,但在处理数据触发器时遇到了困难.我想将触发条件的值绑定到我拥有的某个对象:

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

但是,我不被允许,因为似乎不可能对 Value 属性使用绑定.是吗?我能以某种方式实现这一目标吗?我收到以下错误:

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:

无法在DataTrigger"类型的Value"属性上设置Binding".只能在 DependencyObject 的 DependencyProperty 上设置绑定".

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

推荐答案

不,这是不可能的.正如错误消息所说,只有依赖属性才能成为 WPF 绑定的目标,而 DataTrigger.Value 不是依赖属性.因此,您需要分配一个实际值.

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.

解决方法是使用 MultiBinding,其子 Bindings 是您要比较的两个绑定,并使用 IMultiValueConverter,如果两个输入相等,则返回 true,如果它们不相等,则返回 false.然后 DataTrigger 可以使用该 MultiBinding 和 True 值.

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.

这篇关于对 DataTrigger 条件的 Value 属性使用绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆