EventTrigger
must be defined within a <Style>
element. An EventTrigger
may be defined in either a <Style>
element, or a control's Triggers
property.<Trigger>
elements may contain any number of <Setter>
elements. These elements are responsible for setting properties on the containing element when the <Trigger>
element's condition is met.<Setter>
element will not take effect, even if the trigger condition has been met. Consider the markup <TextBlock Text="Sample">
. The Text
property of the proceeding code will never change based on a trigger because root property definitions take precidence over properties defined in styles.The simplest of the five trigger types, the Trigger
is responsible for setting properties based on other properties within the same control.
<TextBlock>
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Text" Value="Pass">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
In this example, the foreground color of the TextBlock
will turn green when it's Text
property is equal to the string "Pass"
.
A MultiTrigger
is similar to a standard Trigger
in that it only applies to properties within the same control. The difference is that a MultiTrigger
has multiple conditions which must be satisfied before the trigger will operate. Conditions are defined using the <Condition>
tag.
<TextBlock x:Name="_txtBlock" IsEnabled="False">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Text" Value="Pass"/>
<Condition Property="IsEnabled" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="Green"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Notice the MultiTrigger
will not activate until both conditions are met.
A DataTrigger
can be attached to any property, be it on it's own control, another control, or even a property in a non UI class. Consider the following simple class.
public class Cheese
{
public string Name { get; set; }
public double Age { get; set; }
public int StinkLevel { get; set; }
}
Which we will attach as the DataContext
in the following TextBlock
.
<TextBlock Text="{Binding Name}">
<TextBlock.DataContext>
<local:Cheese Age="12" StinkLevel="100" Name="Limburger"/>
</TextBlock.DataContext>
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding StinkLevel}" Value="100">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
In the preceeding code, the TextBlock.Foreground
property will be Green. If we change the StinkLevel
property in our XAML to anything other than 100, the Text.Foreground
property will revert to it's default value.