MCTS Self-Paced Training: Styles and Animation in Windows Presentation Foundation

  • 7/9/2008

Triggers

Along with Setters, Triggers make up the bulk of objects that you use in creating styles. Triggers allow you to implement property changes declaratively in response to other property changes that would have required event-handling code in Windows Forms programming. There are five kinds of Trigger objects, as listed in Table 7-2.

Table 7-2 Types of Trigger Objects

Type

Class Name

Description

Property trigger

Trigger

Monitors a property and activates when the value of that property matches the Value property.

Multi-trigger

MultiTrigger

Monitors multiple properties and activates only when all the monitored property values match their corresponding Value properties.

Data trigger

DataTrigger

Monitors a bound property and activates when the value of the bound property matches the Value property.

Multi-data-trigger

MultDataTrigger

Monitors multiple bound properties and activates only when all the monitored bound properties match their corresponding Value properties.

Event trigger

EventTrigger

Initiates a series of Actions when a specified event is raised.

A Trigger is active only when it is part of a Style.Triggers collection—with one exception. EventTrigger objects can be created within a Control.Triggers collection outside a Style. The Control.Triggers collection can accommodate only EventTriggers, and any other Trigger placed in this collection causes an error. EventTriggers are primarily used with animation and are discussed further in Lesson 2 of this chapter, “Animations.”

Property Triggers

The most commonly used type of Trigger is the property trigger. The property trigger monitors the value of a property specified by the Property property. When the value of the specified property equals the Value property, the Trigger is activated. Important properties of property triggers are shown in Table 7-3.

Table 7-3 Important Properties of Property Triggers

Property

Description

EnterActions

Contains a collection of Action objects that are applied when the Trigger becomes active. Actions are discussed in greater detail in Lesson 2 of this chapter.

ExitActions

Contains a collection of Action objects that are applied when the Trigger becomes inactive. Actions are discussed in greater detail in Lesson 2 of this chapter.

Property

Indicates the property that is monitored for changes.

Setters

Contains a collection of Setter objects that are applied when the Trigger becomes active.

Value

Indicates the value that is compared to the property referenced by the Property property.

Triggers listen to the property indicated by the Property property and compare that property to the Value property. When the referenced property and the Value property are equal, the Trigger is activated. Any Setter objects in the Setters collection of the Trigger are applied to the style, and any Actions in the EnterActions collections are initiated. When the referenced property no longer matches the Value property, the Trigger is inactivated. All Setter objects in the Setters collection of the Trigger are inactivated, and any Actions in the ExitActions collection are initiated.

The following example demonstrates a simple Trigger object that changes the FontWeight of a Button element to Bold when the mouse enters the Button:

<Style.Triggers>
   <Trigger Property="Button.IsMouseOver" Value="True">
      <Setter Property="Button.FontWeight" Value="Bold" />
   </Trigger>
</Style.Triggers>

In this example, the Trigger defines one Setter in its Setters collection. When the Trigger is activated, that Setter is applied.

Multi-triggers

Multi-triggers are similar to property triggers in that they monitor the value of properties and activate when those properties meet a specified value. The difference is that multi-triggers are capable of monitoring several properties at a single time and they activate only when all monitored properties equal their corresponding Value properties. The properties that are monitored and their corresponding Value properties are defined by a collection of Condition objects. The following example demonstrates a MultiTrigger that sets the Button.FontWeight property to Bold only when the Button is focused and the mouse has entered the control:

<Style.Triggers>
   <MultiTrigger>
      <MultiTrigger.Conditions>
         <Condition Property="Button.IsMouseOver" Value="True" />
         <Condition Property="Button.IsFocused" Value="True" />
      </MultiTrigger.Conditions>
      <MultiTrigger.Setters>
         <Setter Property="Button.FontWeight" Value="Bold" />
      </MultiTrigger.Setters>
   </MultiTrigger>
</Style.Triggers>

Data Triggers and Multi-data-triggers

Data triggers are similar to property triggers in that they monitor a property and activate when the property meets a specified value, but they differ in that the property they monitor is a bound property. Instead of a Property property, data triggers expose a Binding property that indicates the bound property to listen to. The following shows a data trigger that changes the Background property of a Label to Red when the bound property CustomerName equals “Fabrikam”:

<Style.Triggers>
   <DataTrigger Binding="{Binding Path=CustomerName}" Value="Fabrikam">
      <Setter Property="Label.Background" Value="Red" />
   </DataTrigger>
</Style.Triggers>

Multi-data-triggers are to data triggers as multi-triggers are to property triggers. They contain a collection of Condition objects, each of which specifies a bound property via its Binding property and a value to compare to that bound property. When all the conditions are satisfied, the MultiDataTrigger activates. The following example demonstrates a MultiDataTrigger that sets the Label.Background property to Red when CustomerName equals “Fabrikam” and OrderSize equals 500:

<Style.Triggers>
   <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
         <Condition Binding="{Binding Path=CustomerName}" Value="Fabrikam" />
         <Condition Binding="{Binding Path=OrderSize}" Value="500" />
      </MultiDataTrigger.Conditions>
      <MultiDataTrigger.Setters>
         <Setter Property="Label.Background" Value="Red" />
      </MultiDataTrigger.Setters>
   </MultiDataTrigger>
</Style.Triggers>

Event Triggers

Event triggers are different from the other Trigger types. While other Trigger types monitor the value of a property and compare it to an indicated value, event triggers specify an event and activate when that event is raised. In addition, event triggers do not have a Setters collection—rather, they have an Actions collection. Although you have been exposed briefly to the SoundPlayerAction in Chapter 4,“Adding and Managing Content,” most actions deal with animations, which are discussed in detail in Lesson 2 of this chapter. The following two examples demonstrate the EventTrigger class. The first example uses a SoundPlayerAction to play a sound when a Button is clicked:

<EventTrigger RoutedEvent="Button.Click">
   <SoundPlayerAction Source="C:\myFile.wav" />
</EventTrigger>

The second example demonstrates a simple animation that causes the Button to grow in height by 200 units when clicked:

<EventTrigger RoutedEvent="Button.Click">
   <EventTrigger.Actions>
      <BeginStoryboard>
         <Storyboard>
            <DoubleAnimation Duration="0:0:5"
               Storyboard.TargetProperty="Height" To="200" />
         </Storyboard>
      </BeginStoryboard>
   </EventTrigger.Actions>
</EventTrigger>