views:

202

answers:

1

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?

+1  A: 

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,

Cheers, Anvaka.

Anvaka