Using Triggers for Specifying WPF User Experience Elements
In a nutshell, WPF designer can use three types of triggers: Event triggers, Property triggers and Data triggers.
Event triggers – Most useful for UX designers, these triggers come into play (invoked is the technical term) when a routed event is raised. We will demonstrate the power of Event triggers that use the ‘Action’s specifications to initiate Animations in WPF in next blog.
Property triggers – Also popular in UX specifications, property triggers come into play when a dependency property is changed. A property trigger (0f Trigger Class)lists all the Setters that need to be executed when a specified property has a specified value. Once the property loses that value, the Setters cannot be in effect.
Suppose, as a designer, you want to demonstrate a button that displays ‘On Hover’ responsiveness. The property you could look for and set trigger against is “IsMouseOver”.
So, here is what you can do.
<Style x:Key=”Hover_Butoon” TargetType={x:Type Button}”>
<Style.Triggers>
<Trigger Property=”IsMouseOver” Value=”True”>
<Setter Property=”BorderBrush” Value=”#FFCE5555”>
</Trigger>
<Style.Triggers>
<Setter Property=”Background” Value=”Gray” />
<Setter Property = “Foreground” Value=”Black />
</Style>
In this case, when the user hovers over the button, the trigger sets in and renders a light orangish border around the button to show that the button is being hovered on.
Data triggers - This type of triggers are actually more generic in nature than event triggers in that they can be triggered by any .NET property. The following TextBox has a Style that triggers the setting of IsEnabled based on the value of its Text property - which is not a dependent property. When Text is the string “Disabled,” IsEnabled is set to false.
<StackPanel Width=”400
<StackPanel. Resources>
<Style TargetType = “{x: Type TextBox}”>
<Style. Triggers >
<DataTrigger
Binding= {Binding RelativeSource={RelativeSource Self }, Path=Text}”
Value=”disabled”>
<Settter Property=”IsEnabled” Value=”False”/>
</DataTrigger>
</Style. Triggers>
<Setter Property=”Background”
Value=”{Binding RelativeSource={RelativeSource Self}, Path=Text}” />
</Style>
</StackPanel.Resources>
<TextBox Margin=”5”/>
</StackPanel>

Comments
Can we use Triggers in Silverlight too?
Posted by: Silverlight Travel | November 10, 2008 07:01 AM
No, Silverlight doesn't support Triggers as yet.
Posted by: Atul Gupta | November 17, 2008 05:03 AM