WPF Triggers

Triggers are a very important feature of WPF that helps to change the visual effect of a framework element or control. Triggers are used in styles to change the visual effect of a WPF element when a property or data is changed or an event is fired. For changing the visual effect of a WPF element, triggers reduce the code in code behind.

For example, let's say you have a rectangle control. You want to change the background color of that control when the mouse hovers over the rectangle control and revert back when mouse leaves. For this you need to code in the mouse hover event and mouse leave event of the rectangle control in the backend class for changing the color of rectangle as in the following code.
Example 1
  1. private void Rectangle_MouseMove_1(object sender, MouseEventArgs e)  
  2. {  
  3.     this.rctback.Fill = Brushes.Red;  
  4. }  
  5.  
  6. private void Rectangle_MouseLeave(object sender, MouseEventArgs e)  
  7. {  
  8.     this.rctback.Fill = Brushes.Green;  
  9. }  
In the preceding code you have filled the background color of the Rectangle control by writing code in two different events, but a trigger helps to overcome this problem by reducing the code. See the following example.
Example 2 
  1. <Rectangle Name="rctback" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Margin="178,92,0,0" >  
  2.             <Rectangle.Style>  
  3.                 <Style TargetType="Rectangle">  
  4.                     <Setter Property="Fill" Value="Green"></Setter>  
  5.                     <Style.Triggers>  
  6.                         <Trigger Property="IsMouseOver" Value="True">  
  7.                             <Setter Property="Fill" Value="Red" />  
  8.                         </Trigger>  
  9.                     </Style.Triggers>  
  10.                 </Style>  
  11.             </Rectangle.Style>  
  12. </Rectangle>  
 There are three types of triggers available in WPF.

  1. Trigger or Property trigger
  2. Data Trigger
  3. Event Trigger 

Trigger or Property Trigger

It's a default trigger of WPF and tagged as <Trigger>. This trigger is fired when the property of an owned control is changed.

Example

  1. <Rectangle Name="rctback" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Margin="178,92,0,0" >  
  2.             <Rectangle.Style>  
  3.                 <Style TargetType="Rectangle">  
  4.                     <Setter Property="Fill" Value="Green"></Setter>  
  5.                     <Style.Triggers>  
  6.                         <Trigger Property="IsMouseOver" Value="True">  
  7.                             <Setter Property="Fill" Value="Red" />  
  8.                         </Trigger>  
  9.                     </Style.Triggers>  
  10.                 </Style>  
  11.             </Rectangle.Style>  
  12. </Rectangle>  

Data Trigger

This trigger is tagged as <DataTrigger> and fires when the data bound with the control is changed.

  1. <Rectangle Name="rctback" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Margin="178,92,0,0" >  
  2.             <Rectangle.Style>  
  3.                 <Style TargetType="Rectangle">  
  4.                     <Setter Property="Fill" Value="Green"></Setter>  
  5.                     <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">  
  6.                         <Setter Property="Fill" Value="Red" />  
  7.                     </DataTrigger>  
  8.                 </Style>  
  9.             </Rectangle.Style>  
  10.         </Rectangle>  

Event Trigger

An Event trigger is fired when an action is done with the control and tagged as <EventTrigger>.

  1. <Rectangle Name="rctback" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Margin="178,92,0,0" >  
  2.     <Rectangle.Style>  
  3.         <Style TargetType="Rectangle">  
  4.             <Setter Property="Fill" Value="Green"></Setter>  
  5.             <Style.Triggers>  
  6.                 <EventTrigger RoutedEvent="MouseEnter">  
  7.                     <EventTrigger.Actions>  
  8.                         <BeginStoryboard>  
  9.                             <Storyboard>  
  10.                                 <DoubleAnimation Duration="0:0:0.800" Storyboard.TargetProperty="Height" To="300" />  
  11.                                 <DoubleAnimation Duration="0:0:0.800" Storyboard.TargetProperty="Width" To="300" />  
  12.                             </Storyboard>  
  13.                         </BeginStoryboard>  
  14.                     </EventTrigger.Actions>  
  15.                      
  16.                 </EventTrigger>  
  17.                 <EventTrigger RoutedEvent="MouseLeave">  
  18.                     <EventTrigger.Actions>  
  19.                         <BeginStoryboard>  
  20.                             <Storyboard>  
  21.                                 <DoubleAnimation Duration="0:0:0.800" Storyboard.TargetProperty="Height" To="100" />  
  22.                                 <DoubleAnimation Duration="0:0:0.800" Storyboard.TargetProperty="Width" To="100" />  
  23.                             </Storyboard>  
  24.                         </BeginStoryboard>  
  25.                     </EventTrigger.Actions>  
  26.                 </EventTrigger>  
  27.             </Style.Triggers>  
  28.         </Style>  
  29.     </Rectangle.Style>  
  30. </Rectangle>  
Summary

I hope you now understand WPF triggers and types of triggers.