WPF在绑定失败时应用触发器 [英] WPF Applying a trigger on binding failure

查看:183
本文介绍了WPF在绑定失败时应用触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这一个的问题。

我绑定到异构的对象集合,并非所有对象都具有相同的属性集。我在一个datagrid中这样做。如果绑定失败,我想灰色单元格。如果绑定失败,是否有办法应用触发器?

I am binding to a heterogeneous collection of objects, not all objects have the same set of properties. I am doing this in a datagrid. I would like to gray out the cell if the binding fails. Is there a way to apply a trigger if a binding fails?

编辑:下面的答案适合我的目的,但是我跟着这个问题,因为我想知道如何做(以非黑客的方式 - 我真的爱黑客但是,不要误会我)

The answer below was suitable for my purposes, but i followed up with this question because I would like to know how to do it (in a non hack fashion - i do love the hack however, don't get me wrong)

推荐答案

据我所知,你不能直接做到这一点。但是,如果您可以识别一个永远不会从成功绑定返回的值,则可以创建一个DataTrigger,其绑定具有该值作为其FallbackValue,并触发相同的值:

As far as I know, you can't do this directly. However, if you can identify a value that will never be returned from successful bindings, you can create a DataTrigger whose binding has that value as its FallbackValue, and trigger on that same value:

<!-- Hibble returns only positive values -->
<DataTrigger Binding="{Binding Hibble, FallbackValue=-1}" Value="-1">
  <Setter Property="Background" Value="Red" />
</DataTrigger>

理论上可能会省略FallbackValue并在 {x :Static DependencyProperty.UnsetValue} ,这将会更加清晰,但这在实践中似乎不起作用。

In theory it might be possible to omit the FallbackValue and trigger on {x:Static DependencyProperty.UnsetValue}, which would be much cleaner, but this doesn't appear to work in practice.

这篇关于WPF在绑定失败时应用触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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