What are the different triggers in WPF? How do they differ and when should I use them?
I\'ve seen the following triggers:
A Trigger
is typically used in a Style
or ControlTemplate
. It triggers on 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 use Triggers?
Triggers are used in styles to perform actions when a property value changes or when an 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 there in WPF?
There are five types of triggers supported by WPF; they are:
Property Trigger
The simplest form of a trigger is a property trigger; it 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
".
Property Trigger
Executes Collections of Setters, when UIElements property value changes.
To create a property trigger on any controls, you have to set trigger in style of the control.
In the above code, Trigger
is created on Button. It will set Opacity
to 0.5 when the button's IsPressed
property changes. You can set a trigger on any Dependency Property of the control.
e.g.
MultiTrigger
MultiTrigger is used to set an action on Multiple Property change. It will execute when all conditions are satisfied within MulitTrigger
.Condition.
Event Trigger
Event Trigger
is used to perform an action when RoutedEvent
of FrameworkElement is raised.Event Trigger
is generally used to perform some animation on a control (like colorAnimation, doubleAnumation using KeyFrame, etc.)
Let's first understand Storyboard
and Animation
.
Animations
:
An animation can provide user interface more attractive with look and feel. We can also create visual effects on the control, Animation can be any type like:
change background color of the control rotate screen in 90 degree angle move object from one location to another Change Opacity (FadeIn/FadeOut) of the circle.
Animation is used with Property of the UIElement. WPF provides different types of animation used with properties, like:
ColorAnimation
:
used to animate/change the color property (SolidColorBrush, LinearGradientBrush) of the UIElement over specific Duration. It has two properties :
From(source) and To(target)
Example 2:
Example 3:
DataTrigger
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. As the name suggests, DataTrigger
applies property value to perform action on Data that Binding to the UIElement.
DataTrigger
allows to set property value when Binding Data matches specified condition. For example:
MultiDataTrigger
MultiTrigger
and MultiDataTrigger
are the same, except they allow you to specify multiple conditions (properties or bindings respectively) and take effect only when all conditions are satisfied.