WPF Styles and Control Templates—Multi-Condition Property Trigger


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

WPF Styles and Control Templates

© 2005 O'Reilly Media

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,(the null value is set via a XAML markup extension) 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.


Image:ProgWPFfig5-10.jpg
Figure 5-10. Multi-condition property trigger with hovering and null content


Image:ProgWPFfig5-11.jpg
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.


Previous_Page_.gif Next_Page_.gif





Personal tools