MultiTrigger 是否否定条件元素

发布于 2024-08-22 07:58:41 字数 1065 浏览 1 评论 0原文

我目前正在学习 WPF 以及使用 MultiTrigger 和 Conditions 来设置样式控件的一些属性。我知道 MultiTrigger 的条件必须全部满足(AND 运算符)才能设置 Setter 指定的值。

但是,如果不满足该值,是否存在条件(让我们将其命名为 NotCondition)。我有一个小例子来说明我的意思。

如果鼠标位于控件上且内容为'Hello World',则背景属性应设置为'Red'。 另一种情况是,如果鼠标悬停并且内容不是'Hello World',则背景应为'蓝色'

 <MultiTrigger>
   <MultiTrigger.Conditions>
     <Condition Property="IsMouseOver" Value="True" />
     <Condition Property="Content" Value="Hello World" />
   </MultiTrigger.Conditions>
   <Setter Property="Background" Value="Red"/>
 </MultiTrigger>
 <MultiTrigger>
   <MultiTrigger.Conditions>
     <Condition Property="IsMouseOver" Value="True" />
     <!--<NotCondition Property="Content" Value="Hello World" />-->
   </MultiTrigger.Conditions>
   <Setter Property="Background" Value="Blue"/>
 </MultiTrigger>

我如何在 WPF/XAML 中存档类似的内容?是否存在 NotCondition-Element 或 Condition-Element 上的属性来否定比较?

I´m currently learning WPF and the use of MultiTrigger and Conditions to set some properties of the styled control. I know that the Conditions of an MultiTrigger must all met (AND-Operator) to set the value specified by the Setter.

But does there exists a Condition if the value is not met (Lets name it a NotCondition). I have a small example to illustrate what i mean.

The Background-Property should be set to 'Red' if the mouse is over the control and the content is 'Hello World'.
The other case is, if the mouse is over and the content is not 'Hello World', the background should be 'Blue'.

 <MultiTrigger>
   <MultiTrigger.Conditions>
     <Condition Property="IsMouseOver" Value="True" />
     <Condition Property="Content" Value="Hello World" />
   </MultiTrigger.Conditions>
   <Setter Property="Background" Value="Red"/>
 </MultiTrigger>
 <MultiTrigger>
   <MultiTrigger.Conditions>
     <Condition Property="IsMouseOver" Value="True" />
     <!--<NotCondition Property="Content" Value="Hello World" />-->
   </MultiTrigger.Conditions>
   <Setter Property="Background" Value="Blue"/>
 </MultiTrigger>

How can i archive something like this in WPF/XAML? Is there a NotCondition-Element or an Attribute on the Condition-Element to negate the compare?

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

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

发布评论

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

评论(1

旧街凉风 2024-08-29 07:58:41

在这种特殊的颜色情况下,您可以使用触发器优先级。例如,

<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="Background" Value="Blue"/>
</Trigger>
<MultiTrigger>
 <MultiTrigger.Conditions>
  <Condition Property="IsMouseOver" Value="True"/>
  <Condition Property="Text" Value="Hello world"/>
 </MultiTrigger.Conditions>
 <Setter Property="Background" Value="Red"/>
</MultiTrigger>

当鼠标悬停且文本为 Hello world 时,最新触发器会覆盖第一个触发器的效果。

WPF 中没有任何内置功能可以让您执行条件触发器,但是Mike Hillberg 针对此问题提出了一种非常有趣的解决方案:一个可比较的 DataTrigger

希望这有帮助

In this particular case with colors you can use triggers precedence. E.g.

<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="Background" Value="Blue"/>
</Trigger>
<MultiTrigger>
 <MultiTrigger.Conditions>
  <Condition Property="IsMouseOver" Value="True"/>
  <Condition Property="Text" Value="Hello world"/>
 </MultiTrigger.Conditions>
 <Setter Property="Background" Value="Red"/>
</MultiTrigger>

Latest trigger overrides effect from the first trigger when both mouse is over and text is Hello world.

There is nothing build-in in WPF that lets you do conditional triggers, but Mike Hillberg has suggested one very interseting solution for this problem: A Comparable DataTrigger

Hope this helps

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