Triggers are one of the sweet parts of XAML and Windows
Presentation Foundation. They save you from coding many user interface level
events. Actually, triggers help in changing an attribute of an element when a
condition is true.
For example, when the mouse is over a Button, by using
triggers you can change its background dynamically on runtime to your desire
color.
Each style can have a collection of triggers for different
attributes of an element. As triggers will be declared inside styles, all of
what I said about styles can be applied to triggers as well. So you can
declare a trigger for a specific element locally, all elements on a page or a
special kind of element.
Listing 9, Figure 6 and Figure 7 represent the use of
triggers for a page to change the background of a Button once the mouse is over
it.
Listing 9
<Window x:Class="ResourcesInXAML.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Resources In XAML" Height="150" Width="300"
>
<Window.Resources>
<Style x:Key="myStyle">
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel Margin="30">
<Button Style="{StaticResource myStyle}"
Width="200" Height="40">
ASP Alliance
</Button>
</StackPanel>
</Window>
Figure 6
Figure 7