Looking at Triggers with Styles and Control Templates - Multiple Triggers
(Page 3 of 4 )
While you can set as many properties as you like in a property trigger, there can be more than one trigger in a style. When grouped together under the Style.Triggers element, multiple triggers act independently of each other.
For example, we can update our code so that if the mouse is hovering over one of our buttons, it’ll be colored yellow and if the button has focus (the tab and arrow keys move focus around), it’ll be colored green, as in Example 5-26. Figure 5-9 shows the result of one cell having focus and another with the mouse hovering.
Example 5-26. Multiple property triggers
<Style TargetType="{x:Type Button}">
...
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True" >
<Setter Property="Background" Value="Yellow" />
</Trigger>
<Trigger Property="IsFocused" Value="True" >
<Setter Property="Background" Value="LightGreen" />
</Trigger>
</Style.Triggers>
</Style>

Figure 5-9. Multiple property triggers in action
If multiple triggers set the same property, the last one wins. For example, in Figure 5-9, if a button has focus and the mouse is over it, the background will be light green because the trigger for theIsFocusedtrigger is last in the list of triggers.
Multi-Condition Property Trigger
If you’d like to check more than one property before a trigger condition is activated—e.g., the mouse is hovering over a button and the button content is empty—you can combine multiple conditions with a multiple-condition property trigger, as in Example 5-27.
Example 5-27. A multi-property trigger
<Style TargetType="{x:Type Button}">
...
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="Content" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Yellow" />
</MultiTrigger>
</Style.Triggers>
</Style>
Multi-condition property triggers check all of the properties’ values to be set as specified, not just one of them. Here, we’re watching for both a mouse hover and for the content to be null,* reflecting the game logic that only clicking on an empty cell will result in a move.
Figure 5-10 shows the yellow highlight on an empty cell when the mouse hovers, and Figure 5-11 shows the yellow highlight absent when the mouse hovers over a full cell.

Figure 5-10. Multi-condition property trigger with hovering and null content

Figure 5-11. Multi-condition property trigger not triggering as content is not null
Property triggers are great for noticing when the user is interacting with a control displaying your program’s state. However, we’d also like to be able to notice when the program’s state itself changes, such as when a particular player makes a move, and update our style settings accordingly. For that, we have data triggers.
Next: Data Triggers >>
More XML Articles
More By O'Reilly Media
|
This article is excerpted from chapter five of the book Programming Windows Presentation Foundation, written by Chris Sells and Ian Griffiths (O'Reilly; ISBN: 0596101139). Check it out today at your favorite bookstore. Buy this book now.
|
|