XML
  Home arrow XML arrow Page 4 - 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Looking at Triggers with Styles and Control Templates - Data Triggers


    (Page 4 of 4 )

    Unlike property triggers, which check only WPF dependency properties, data triggers can check any old .NET object property. While property triggers are generally used to check WPF visual-element properties, data triggers are normally used to check the properties of non-visual objects used as content, such as our PlayerMove objects in Example 5-28.

    Example 5-28.  Two data triggers

    <Window.Resources>
      <Style TargetType="{x:Type Button}">
       
    ...
      </Style>
      <Style x:Key="CellTextStyle" TargetType="{x:Type TextBlock}">
       
    ...
        <Style.Triggers>
         
    <DataTrigger Binding="{Binding Path=PlayerName}" Value="X">
           
    <Setter Property="Foreground" Value="Red" />
          </DataTrigger>
          <DataTrigger Binding="{Binding Path=PlayerName}" Value="O">
            <Setter Property="Foreground" Value="Green" />
          </DataTrigger>
        </Style.Triggers>
      </Style>
      <Style x:Key="MoveNumberStyle" TargetType="{x:Type TextBlock}">
        ...
        </Style>
        ...
        <DataTemplate DataType="{x:Type l:PlayerMove}">
         
    <Grid>
            <TextBlock
              TextContent="{Binding Path=PlayerName}" 
             
    Style="{StaticResource CellTextStyle}" />
            <TextBlock
              TextContent="{Binding Path=MoveNumber}"
              Style="{StaticResource MoveNumberStyle}" />
         
    </Grid>
        </DataTemplate>
    </Window.Resources>

    DataTriggerelements go under theStyle.Triggerselement just like property triggers and, just like property triggers, there can be more than one of them active at any one time. While a property trigger operates on the properties of the visual elements displaying the content, a data trigger operates on the content itself. In our case, the content of each of the cells is aPlayerMove object. In both of our data triggers, we’re binding to thePlayerName property. If the value isX, we’re setting the foreground to red and if it’sO, we’re setting it to green.

    Take care where you put the data trigger. In our example, we’ve got theButton-type style and the namedCellTextStylestyle as potential choices. I’ve written this chapter twice now and both times I’ve initially put the data trigger on the button style instead of on the content in the data template. Data triggers are based on content, so make sure you put them into your content styles, not your control styles.

    We haven’t had per-player colors since we moved to data templates after setting styles programmatically in Figure 5-5, but data triggers bring us that feature right back, along with all of the other features we’ve been building up, as shown in Figure 5-12.

    Unlike property triggers, which rely on the change notification of dependency properties, data triggers rely on an implementation of the standard property-change notification patterns that are built into .NET and are discussed in Chapter 4—e.g.,INotifyPropertyChanged. Since eachPlayerMove object is constant, we don’t need to implement this pattern, but if you’re using data triggers, chances are that you will need to implement it on your custom content classes.

    One other especially handy feature of data triggers is that there’s no need for an explicit check for null content. If the content is null, the trigger condition is automatically false, which is why the application isn’t crashing trying to dereference a nullPlayerMoveto get to thePlayerNameproperty.

     


    Figure 5-12.  Data triggers in action

    Please check back next week for the conclusion to this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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





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