XML
  Home arrow XML arrow Page 2 - Looking at Triggers with Styles and Contro...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Looking at Triggers with Styles and Control Templates
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-06-28

    Table of Contents:
  • Looking at Triggers with Styles and Control Templates
  • Triggers
  • Multiple Triggers
  • Data Triggers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Looking at Triggers with Styles and Control Templates - Triggers


    (Page 2 of 4 )

     

    So far, we’ve se en styles as a collection of Setter elements. When a style is applied, the settings described in the Setter elements are applied unconditionally (unless overridden by per-instance settings). On the other hand, triggers are a way to wrap one or more Setter elements in a condition so that, if the condition is true, the corresponding Setter elements are executed, and when the condition becomes false, the property value reverts to its pre-trigger value.

    WPF comes with three kinds of things that you can check in a trigger condition: a dependency property, a .NET property, and an event. The first two directly change values based on a condition, as I described, while the last, an event trigger, is activated when an event happens and then starts (or stops) an animation that causes properties to change.

    Property Triggers

    The simplest form of a trigger is a property trigger, which 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 so by watching for the IsMouseOver property to have a value of True, as in Example 5-24.

    Example 5-24.  A simple property trigger

    <Style TargetType="{x:Type Button}">
     
    ...
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True" >
          <Setter Property="Background" Value="Yellow" />
        </Trigger>
      </Style.Triggers>
    </Style>

    Triggers are grouped together under the Style.Triggers element. In this case, we’ve added a Trigger element to the button style. When the IsMouseOverproperty of our button is true, theBackgroundvalue of the button will be set to yellow, as shown in Figure 5-8.

    You’ll notice in Figure 5-8 that only the button where the mouse is currently hovering has its background set to yellow, even though other buttons have clearly been under the mouse. There’s no need to worry about setting a property back when the trigger is no longer true—e.g., watching forIsMouseOverto beFalse. The WPF dependency-property system watches for the property trigger to become inactive and reverts to the previous value.


    Figure 5-8.  A Property trigger in action

    Property triggers can be set to watch any of the dependency properties on the control to which your style is targeted and to set any of the dependency properties on the control while the condition is true. In fact, you can use a single trigger to set multiple properties if you like, as in Example 5-25.

    Example 5-25.  Setting multiple properties with a single trigger

    <Style TargetType="{x:Type Button}">
     
    ...
     
    <Style.Triggers>
       
    <Trigger Property="IsMouseOver" Value="True" >
         
    <Setter Property="Background" Value="Yellow" />
          <Setter Property="FontStyle" Value="Italic" /> 
        
    </Trigger>
      </Style.Triggers>
    </Style>

    In Example 5-25, we’re setting the background to yellow and the font style to italic when the mouse is over a button.

    One property that you’re not allowed to set in a trigger is theStyleproperty itself. If you try, you’ll get the following error:

    AStyleobject is not allowed to affect the Style property of the object to which it applies.

    This makes sense. Since it’s theStyle that’s setting the property, and that participates in “unsetting” it when the trigger is no longer true, what sense would it make to change the veryStylethat’s providing this orchestration? This would be somewhat like switching out your skis after you’ve launched yourself off of a jump but before you’ve landed.

    More XML Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Windows Presentation...
     

    Buy this book now. This article is excerpted from chapter five of the book Programming Windows Presentation Foundation, written by Chris Sells and Ian Griffiths (O'Reilly; ISBN: 0596101139). Check it out today at your favorite bookstore. Buy this book now.

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway