Types of Triggers in Windows Presentation Foundation (WPF)

Introduction

Triggers are used to perform certain actions when a specified condition is fulfilled. Triggers are used to create visual effects on controls and framework elements. Triggers are parts of styles and are always defined inside a style.

Types of Triggers

Basically, there are 3 types of triggers, they are:

  • Property Trigger
  • Data Trigger
  • Event Trigger

But a Property Trigger and a Data Trigger have the following two subcategories:

  • Multiple Property Trigger (Multi Trigger)
  • MultiData Trigger

Now we learn about each trigger one by one.

Property Trigger

This Trigger is activated when the UIElements property matches a specified value. A Property Trigger is the most commonly used trigger among all triggers. It is defined within a <Trigger> element.

XAML Code

  1. <Window x:Class="WpfApplication26.MainWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.   
  5.     Title="PropertyTriggersSample" Height="288" Width="450" >  
  6.     <Grid Margin="0,0,-8,-11">  
  7.         <TextBlock Text="I Like C# Corner" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center">  
  8.             <TextBlock.Style>  
  9.                 <Style TargetType="TextBlock">  
  10.                     <Setter Property="Foreground" Value="Green"></Setter>  
  11.                     <Style.Triggers>  
  12.                         <Trigger Property="IsMouseOver" Value="True">  
  13.                             <Setter Property="Foreground" Value="Blue" />  
  14.                             <Setter Property="TextDecorations" Value="Underline" />  
  15.                         </Trigger>  
  16.                     </Style.Triggers>  
  17.                 </Style>  
  18.             </TextBlock.Style>  
  19.         </TextBlock>  
  20.     </Grid>  
  21. </Window>  
In the preceding code, when the IsMouseOver property value is true, the defined setter action inside the trigger will be executed and the Foreground property of the textblock will turn Blue with an underline.

Multiple Property Trigger (Multi Trigger)

This is similar to a Trigger but it combines multiple conditions to be met simultaneously. It will execute when all the conditions are satisfied within <MultiTrigger.Conditions>.

XAML Code

  1. <Window x:Class="WpfApplication27.MainWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.   
  5.     Title="MultiTriggerSample" Height="350" Width="525">  
  6.     <Window.Resources>  
  7.         <Style TargetType="Button">  
  8.             <Style.Triggers>  
  9.                 <MultiTrigger>  
  10.                     <MultiTrigger.Conditions>  
  11.                         <Condition Property="IsMouseOver" Value="True" />  
  12.                         <Condition Property="IsPressed" Value="True" />  
  13.                     </MultiTrigger.Conditions>  
  14.                     <MultiTrigger.Setters>  
  15.                         <Setter Property="Foreground" Value="Blue" />  
  16.                     </MultiTrigger.Setters>  
  17.                 </MultiTrigger>  
  18.             </Style.Triggers>  
  19.         </Style>  
  20.     </Window.Resources>  
  21.     <StackPanel>  
  22.         <Button Content="Click Me!" IsDefault="True"  
  23.   
  24. FontSize="20" Height="64" Margin="186,5,145,5" />  
  25.     </StackPanel>  
  26. </Window>  
In the preceding code, when the IsMouseOver property and the IsPressed both value are both true, the defined setter action inside the MultiTrigger will be executed and the Foreground property of the Button will turn Blue.

Data Trigger

A Data Trigger is activated when the binding data matches the specified conditions. In the Data trigger we need to specify the binding instead of property name.

XAML Code
  1. <Window x:Class="WpfApplication27.MainWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.   
  5.     Title="DataTriggerSample" Height="350" Width="525">  
  6.     <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="39,47,22,57" Width="456" Height="216">  
  7.         <TextBlock FontSize="25" Foreground="Blue" Margin="25,0,0,0">Do you Like C# Corner ?</TextBlock>  
  8.         <CheckBox Name="cbSample" Content="Yes" FontSize="20" Margin="38,0,0,0"/>  
  9.         <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48">  
  10.             <TextBlock.Style>  
  11.                 <Style TargetType="TextBlock">  
  12.                     <Setter Property="Text" Value="No" />  
  13.                     <Setter Property="Foreground" Value="Red" />  
  14.                     <Style.Triggers>  
  15.                         <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">  
  16.                             <Setter Property="Text" Value="Yes!" />  
  17.                             <Setter Property="Foreground" Value="Green" />  
  18.                         </DataTrigger>  
  19.                     </Style.Triggers>  
  20.                 </Style>  
  21.             </TextBlock.Style>  
  22.         </TextBlock>  
  23.     </StackPanel>  
  24. </Window>  
In the preceding code when the IsChecked property of CheckBox is true, the text will become Green, saying "Yes!".

MultiData Trigger

This is similar to a Trigger but it combines multiple conditions to be met simultaneously. It will execute when all the conditions are satisfied within the <MultiDataTrigger.Conditions>.

XAML Code
  1. <Window x:Class="WpfApplication27.MainWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.   
  5.     Title="MultiDataTriggerSample" Height="350" Width="525">  
  6.     <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">  
  7.         <TextBlock FontSize="25" Foreground="Blue">Do You Want to Register on C# Corner ?</TextBlock>  
  8.         <CheckBox Name="cbSampleYes" Content="I Love Coding" FontSize="20" />  
  9.         <CheckBox Name="cbSampleSure" Content="I Want to Register" FontSize="20" />  
  10.         <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="28">  
  11.             <TextBlock.Style>  
  12.                 <Style TargetType="TextBlock">  
  13.                     <Setter Property="Text" Value="You are not Registered" />  
  14.                     <Setter Property="Foreground" Value="Red" />  
  15.                     <Style.Triggers>  
  16.                         <MultiDataTrigger>  
  17.                             <MultiDataTrigger.Conditions>  
  18.                                 <Condition Binding="{Binding ElementName=cbSampleYes, Path=IsChecked}" Value="True" />  
  19.                                 <Condition Binding="{Binding ElementName=cbSampleSure, Path=IsChecked}" Value="True" />  
  20.                             </MultiDataTrigger.Conditions>  
  21.                             <Setter Property="Text" Value="You are Registered" />  
  22.                             <Setter Property="Foreground" Value="Green" />  
  23.                         </MultiDataTrigger>  
  24.                     </Style.Triggers>  
  25.                 </Style>  
  26.             </TextBlock.Style>  
  27.         </TextBlock>  
  28.     </StackPanel>  
  29. </Window>  
In the preceding code, once both checkboxes are checked, the text will change from “You are not Registered ” to “You are Registered ”. If you remove a check from any one of them, the text will again change to “You are not Registered”.

Event Trigger

An Event Trigger is activated when a RouteEvent of a Framework Element is raised. It is usually used when the animation is to be called in response to an event like colorAnimation, doubleAnimation and many other animations as needed. Inside the action a Storyboard is required. I will explain about Animation and Storyboard in detail in my next article.

Code Behind

 

  1. <Window x:Class="WpfApplication26.MainWindow"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.   
  5.     Title="EventTriggerSample" Height="288" Width="450" >  
  6.     <Grid>  
  7.         <TextBlock Name="lblStyled" Text="I Like C# Corner" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center">  
  8.             <TextBlock.Style>  
  9.                 <Style TargetType="TextBlock">  
  10.                     <Style.Triggers>  
  11.                         <EventTrigger RoutedEvent="MouseEnter">  
  12.                             <EventTrigger.Actions>  
  13.                                 <BeginStoryboard>  
  14.                                     <Storyboard>  
  15.                                         <DoubleAnimation Duration="0:0:0.200" Storyboard.TargetProperty="FontSize" To="30" />  
  16.                                     </Storyboard>  
  17.                                 </BeginStoryboard>  
  18.                             </EventTrigger.Actions>  
  19.                         </EventTrigger>  
  20.                         <EventTrigger RoutedEvent="MouseLeave">  
  21.                             <EventTrigger.Actions>  
  22.                                 <BeginStoryboard>  
  23.                                     <Storyboard>  
  24.                                         <DoubleAnimation Duration="0:0:0.500" Storyboard.TargetProperty="FontSize" To="18" />  
  25.                                     </Storyboard>  
  26.                                 </BeginStoryboard>  
  27.                             </EventTrigger.Actions>  
  28.                         </EventTrigger>  
  29.                     </Style.Triggers>  
  30.                 </Style>  
  31.             </TextBlock.Style>  
  32.         </TextBlock>  
  33.     </Grid>  
  34. </Window>  
In the preceding code when the mouse enters, the text block changes to a Font Size of 30 pixels. When the mouse leaves, the textblock changes to the Font Size back to 18 pixels, but a bit slower.

I hope this article will able to help beginners in understanding how Triggers work in Windows Presentation Foundation (WPF).

Up Next
    Ebook Download
    View all
    Learn
    View all