DataTrigger 其中值不为空? [英] DataTrigger where value is NOT null?

查看:42
本文介绍了DataTrigger 其中值不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以创建一个 setter 来检查值是否为 NULL 并执行某些操作.示例:

I know that I can make a setter that checks to see if a value is NULL and do something. Example:

<TextBlock>
  <TextBlock.Style>
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding SomeField}" Value="{x:Null}">
          <Setter Property="TextBlock.Text" Value="It's NULL Baby!" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </TextBlock.Style>
</TextBlock>

但是我如何检查not"值...如NOT NULL"或NOT = 3"?这在 XAML 中可行吗?

But how can I check for a "not" value... as in "NOT NULL", or "NOT = 3"? Is that possible in XAML?

结果:感谢您的回答...我知道我可以做一个值转换器(这意味着我必须编写代码,这不会是我希望的纯 XAML).但是,这确实回答了不"您不能在纯 XAML 中做到这一点的问题.然而,所选择的答案可能显示了创建这种功能的最佳方式.很好的发现.

Results: Thanks for your answers... I knew I could do a value converter (which means I would have to go in code, and that would not be pure XAML as I hoped for). However, that does answer the question that effectively "no" you can't do it in pure XAML. The answer selected, however, shows probably the best way to create that kind of functionality. Good find.

推荐答案

我在使用 DataTriggers 时遇到了类似的限制,看起来您只能检查相等性.我见过的最接近可能对您有帮助的方法是进行除相等性以外的其他类型比较的技术.

I ran into a similar limitation with DataTriggers, and it would seem that you can only check for equality. The closest thing I've seen that might help you is a technique for doing other types of comparisons other than equality.

这篇博文 描述了如何进行比较例如 DataTrigger 中的 LT、GT 等.

This blog post describes how to do comparisons such as LT, GT, etc in a DataTrigger.

DataTrigger 的这个限制可以在某种程度上解决,方法是使用转换器将数据转换成一个特殊的值,然后您可以进行比较,如 Robert Macnee 的回答所建议的那样.

This limitation of the DataTrigger can be worked around to some extent by using a Converter to massage the data into a special value you can then compare against, as suggested in Robert Macnee's answer.

这篇关于DataTrigger 其中值不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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