Posted by: Zeeshan Amjad | August 20, 2009

Property Trigger in WPF


There are three types of trigger in WPF named Property Trigger, Data Trigger and Event trigger. All of the triggers are inherited by TriggerBase class. Here is a partial class diagram of TriggerBase class.

Trigger

With the help of trigger, we can set the property changes declaratively when other property changes, data changes or event happened.

Property trigger is probably the most commonly used trigger. It monitor the value of the specified property. Data trigger is quite similar to property trigger and execute when property has some specified value. Event trigger is different then both property and data trigger. Instead of monitoring the value of the property, this trigger execute when some specified event is raised. One typical example of event trigger is animation. If we want to perform some animation at the time of loading the application, then we start that animation with event trigger and select load as an event trigger.

Here is a simple example of Property Trigger.

  1: <Window x:Class="TypeConvertor.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Property Trigger Demo" Height="300" Width="400">
  5:     <Window.Resources>
  6:         <Style TargetType="{x:Type TabItem}">
  7:             <Style.Triggers>
  8:                 <Trigger Property="IsFocused" Value="True">
  9:                     <Setter Property="FontWeight" Value="Bold"/>
 10:                     <Setter Property="Foreground" Value="Blue"/>
 11:                 </Trigger>
 12:             </Style.Triggers>
 13:         </Style>
 14:     </Window.Resources>
 15:     
 16:     <Grid>                
 17:         <TabControl>
 18:             <TabItem Header="Visual C++"></TabItem>
 19:             <TabItem Header="Visual C#"></TabItem>
 20:             <TabItem Header="Visual Basic"></TabItem>
 21:             <TabItem Header="F#"></TabItem>
 22:         </TabControl>
 23:     </Grid>
 24: </Window>
 25: 

Here we are going to define property trigger on tab item. We select two properties when tab item has focus i.e. change the font color and make the font bold. Here is the output of this program when we select second tab.

Trigger_01

Advertisements

Responses

  1. […] animation with Property Trigger We have already saw an example of Property Trigger and animation using XAML. Now we are going to use these two concepts together and apply animation […]

  2. These are the most common and basic types of triggers within the WPF programmatic model. They are

    basically XAML condition statements that check the state of a property to determine whether to use a

    specific style/setter value within the style of the element.

    http://www.mindstick.com/Blog/168/Property%20Triggers%20in%20WPF

  3. Very good explaintation.

    I beleive
    shoudl be

    • Thanks to visit my blog and like it.

      Regards
      Zeeshan Amjad

  4. I beleive

    instead of IsFocused it should be IsSelected

    • Thanks for pointing out the mistake. Yes in this case IsSelected is more appropriate.

      Regards
      Zeeshan Amjad


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: