WPF 在绑定失败时应用触发器

发布于 2024-08-28 05:57:33 字数 394 浏览 4 评论 0原文

这个问题是这个问题的后续问题...

我是绑定到异构对象集合,并非所有对象都具有相同的属性集。我正在数据网格中执行此操作。如果绑定失败,我想使单元格变灰。如果绑定失败,有没有办法应用触发器?

编辑:下面的答案适合我的目的,但我跟进了 这个问题因为我想知道如何做到这一点(以非黑客方式 - 我确实喜欢黑客,但不要误会我的意思)

This question is a follow on from this one...

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?

EDIT: 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-09-04 05:57:33

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

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

理论上,可以省略 FallbackValue 和触发器在 {x:Static DependencyProperty.UnsetValue} 上,这会更干净,但这在实践中似乎不起作用。

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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文