Infosys Microsoft Alliance and Solutions blog

« WPF: Working with command line arguments | Main | Infosys Session on Azure at Microsoft PDC2008 »

Using Triggers for Specifying WPF User Experience Elements

Triggers are an interesting tool for WPF User Experience designers as they give a ‘declarative alternative’ for specifying interactive behavior. Triggers have a collection of Setters just like Style. But whereas Style applies its value unconditionally, a trigger performs it work based on some conditionality.

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>

TrackBack

TrackBack URL for this entry:
http://www.infosysblogs.com/microsoft-mt/mt-tb.fcgi/311

Comments

Can we use Triggers in Silverlight too?

No, Silverlight doesn't support Triggers as yet.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)