These are the most common and basic types of triggers
within the WPF programmatic model. They are
basically XAML condition statements that check the state
of a property to determine whether to use a
specific style/setter value within the style of the
element.
The following are the basic steps for creating a property
trigger:
1.
Create a top-level <Style> XML element in
your XAML page and set the TargetType of the
<Style> XML element to {x:Type Button}:
<Style TargetType=”{x:Type Button}”>
2.
Within
the <Style> entries, define the <Style.Trigger> element:
<Style.Triggers>
3.
Within
the <Style.Triggers> XML element, create a <Trigger> XML element
with the property
to check and the value to compare it to set as attributes
for this class:
<Trigger Property=”IsMouseOver” Value=”False”>
</Trigger>
4.
Enter the
<Setter> XML element to designate the actual property value of the Button
control(s)
that will need to be rendered. In this case, the
developer can set the property to be modified in the trigger’s condition
routine.
<Setter Property=”Background” Value=”Red” />
5.
Add a
button to the XAML page, with no special settings needed:
<Button VerticalAlignment=”Left”
HorizontalAlignment=”Stretch”
Height=”66”
Name=”btnOne”>Button
</Button>
The final syntax should resemble the following:
<Window x:Class=”PropertyTrigger.Window1”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”PropertyTrigger”
>
<Window.Resources>
<Style TargetType=”{x:Type Button}”>
<Style.Triggers>
<Trigger Property=”IsMouseOver” Value=”False”>
<Setter Property=”Background” Value=”Red” />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Button VerticalAlignment=”Left”
HorizontalAlignment=”Stretch”
Height=”66”
Name=”btnOne”>Button
</Button>
</Grid>
</Window>
As displayed, the setter property would apply only when
the trigger is evaluated to be a False condition.
If the trigger is evaluated to a True value, it will not
fire.