Home  separator  Styling  separator  Styles  separator  Triggers
Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


Triggers in Styles

Triggers define a list of setters that are executed if the specified condition is fulfilled. WPF knows three diferent types of triggers

  • Property Triggers get active, when a property gets a specified value.
  • Event Triggers get active, when a specified event is fired.
  • Data Triggers get active, when a binding expression reaches a specified value.

Property Triggers

A property trigger activates a list of setters when a property gets a specified value. If the value changes the trigger undoes the setters.

 
<Style x:Key="styleWithTrigger" TargetType="Rectangle">
    <Setter Property="Fill" Value="LightGreen" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Fill" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
 
<Rectangle Style="{StaticResource styleWithTrigger}"></Rectangle>
 
 

Event Triggers

Data Triggers

Multi Triggers

Trick to use triggers everywhere

Triggers are only available in styles and templates. Even if every control has a property Triggers, it only works with EventTriggers. If you want to use normal triggers outside a style or a template, you can use a little trick. Just wrap your code with the following snippet:

 
<Control>
    <Control.Template>
        <ControlTemplate>
            <!-- Triggers are available here -->
        </ControlTemplate>
    </Control.Template>
</Control>
 
 




Last modified: 2010-11-22 13:11:55
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Ktahl
Commented on 25.August 2010
At least you are honest! You are saving people time by gathering all of this information in one place instead of scattered all over the web.
Wasif
Commented on 9.December 2010
A very useful place for beginner programmers. Very easy to understand stuff.
Eswararao
Commented on 30.December 2010
A very useful place for beginner programmers
sankar
Commented on 15.June 2011
You will get the better idea about trigger from this link http://www.c-sharpcorner.com/UploadFile/raj1979/3637/
Sanjog
Commented on 15.June 2011
Really Good One ..! Verry Easy to undestand for biginners like me !
Thanks a lot.......Christian:)
Timo Paschke
Commented on 22.June 2011
Great work! In this case, I miss the trigger &quot;MultiDataTrigger&quot;. In addition it is woth mentioning that the developer gets no Intellisense-Support for the &quot;Trigger&quot;-Element in the VS-Designer.
Do you have any plans in completing this page soon?
Please keep on writing your helpful introduction to WPF - it makes the first understandings very easy!
Sri Lalitha
Commented on 18.July 2011
Missing Examples on
1. Event Triggers
2. Data Triggers
3. Multi Triggers

Kindly explain with examples
Aditya Kiran
Commented on 10.August 2011
Too good.. very useful for the beginners like me.
Aditya Kiran
Commented on 10.August 2011
Too good.. very useful for the beginners like me.

Name
E-Mail (optional)
Comment