What is a Trigger?
A Trigger is typically used in a Style or ControlTemplate. It triggers on the properties of whatever is being templated, and sets other properties of the control (or of specific template elements). For example, you would use a Trigger on IsMouseOver to respond to the mouse being over the control, and the setters might update a brush to show a "hot" effect.
Why do we use Trigger?
Triggers are used in styles to perform actions on a change of any property value or event fires. Triggers create visual effects on controls. By using Triggers we can change the appearance of Framework Elements.
How many types of triggers are in WPF?
There are five types of triggers supported by WPF; they are,
- Property Trigger
- Data Trigger
- MultiTrigger
- MultiDataTrigger
- Event Trigger
Property Trigger
The simplest form of a trigger is a property trigger that watches for a dependency property to have a certain value.
For example, if we wanted to light up a button in yellow as the user moves the mouse over it, we can do that by watching for the IsMouseOver property to have a value of "True".
This trigger gets active when UI elements property value changes. The below Trigger is created on a Button. It will set Opacity to 0.5; when the button is pressed the property changes.
Data Trigger
DataTriggers are Triggers that watch a bound value instead of a Dependency Property. They allow you to watch a bound expression, and will react when that binding evaluates equal to your value.
For example, you could set a DataTrigger on "{Binding Value}" or "{Binding ElementName=MyTextBox, Path=IsChecked}". You can even use Converters with DataTriggers, such as,
- <DataTrigger Binding="{Binding SomeInt, Converter={StaticResource IsGreaterThanZero}}" Value="True">
Setter objects of the DataTrigger describe property values to apply when binding data matches the condition. Picture is Byte[] property of the class.
If Picture is null then DataTrigger applies on the image to set the image source to noImage.png, otherwise it will set the image source from the picture.
It's the same as if picture is null, it applies TextBlock value to "No Image Availalbe" and foreground color to red, otherwise it sets default value from data.
MultiTrigger
A MultiTrigger is used to set an action on multiple property changes. It will execute when all conditions are satisfied.
For example, suppose we want to change the background color of a TextBox when the mouse moves, and focus and lose focus, for that you can use a MultiTrigger.
- <Window x:Class
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MultiTRiggerDEmo" Height="300" Width="300" Loaded="Window_Loaded">
- <Window.Resources>
- <Style x:Key="MultTrigg" TargetType="{x:Type TextBox}" ="">
- <Style.Triggers>
- <MultiTrigger>
- <MultiTrigger.Conditions>
- <Condition Property="IsFocused" Value="True">
- </Condition>
- <Condition Property="IsMouseOver" Value="True">
- </Condition>
- </MultiTrigger.Conditions>
- <MultiTrigger.Setters>
- <Setter Property="Background" Value="yellow" >
- </Setter>
- </MultiTrigger.Setters>
- </MultiTrigger>
- </Style.Triggers>
- </Style>
- </Window.Resources>
- <StackPanel>
- <TextBox Style="{StaticResource MultTrigg}"></TextBox>
- </StackPanel>
- </Window>

Rafnas T PPosted Jul 1, 2016, 12:14 AM
Its my pleasure
Basil HowitsonPosted Jun 27, 2016, 4:54 AM
Thank you Rafnas. I am only now getting my feet wet with WPF and the explanation in your article has added clarity and understanding regarding the use of available triggers.
kalu singh raoPosted Jun 26, 2016, 2:04 AM
Nice...