XML
  Home arrow XML arrow Looking at Triggers with Styles and Contro...
Iron Speed
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 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
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 / 3
    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
    (Page 1 of 4 )

    Continuing our discussion from yesterday, we'll cover data templates and various kinds of triggers. 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). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Data Templates

    Recall from Chapter 4 that WPF allows you to define a data template, which is a tree of elements to expand in a particular context. Data templates are used to provide an application with the ability to render non-visual objects, as shown in Example 5-22.

    Example 5-22. Setting a PlayerMove data template without styles

    <?Mapping XmlNamespace="l" ClrNamespace="TicTacToe" ?>
    <Window ...
    xmlns:local="local">
      <Window.Resources>
       
    <DataTemplate DataType="{x:Type local:PlayerMove}">
         
    <Grid>
           
    <TextBlock
             
    TextContent="{Binding Path=PlayerName}"
             
    FontSize ="32"
             
    FontWeight="Bold"
             
    VerticalAlignment="Center"
             
    HorizontalAlignment="Center" />
           
    <TextBlock
             
    TextContent="{Binding Path=MoveNumber}"
             
    FontSize="16"
             
    FontStyle="Italic"
             
    VerticalAlignment="Bottom"
             
    HorizontalAlignment="Right" />
          </Grid>
       
    </DataTemplate>
        ...
     
    </Window.Resources>
     
    ...
    </Window>

    Using the XAML mapping syntax introduced in Chapter 1, we’ve mapped thePlayerMovertype into the XAML with the mapping directive and thexmlnsattribute, which we’ve used as the data type of the data template. Now, whenever WPF sees aPlayerMove object, such as the content of all of our buttons, the data template will be expanded. In our case, the template consists of a grid to arrange two text blocks, one showing the player name in the middle of the button and one showing the move number in the bottom right, along with some other settings to make things pretty.

    Data Templates with Style

    However, these property settings are buried inside a data template several layers deep. Just as it’s a good idea to take “magic numbers” out of your code, pulling them out and giving them names for easy maintenance, it’s a good idea to move groups of settings into styles,* as in Example 5-23.

    Example 5-23.  Setting a PlayerMove data template with styles

    <Window.Resources>
     
    <Style x:Key="CellTextStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="32" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
     
    </Style>
      <Style x:Key="MoveNumberStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontStyle" Value="Italic" />
        <Setter Property="VerticalAlignment" Value="Bottom" />
        <Setter Property="HorizontalAlignment" Value="Right" />
      </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>

    It’s common to use styles, which set groups of properties, with data templates, which create groups of elements that have groups of properties. Figure 5-7 shows the result.


    Figure 5-7.  Showing objects of a custom type using templates and styles

    Still, as nice as Figure 5-7 is, the interaction is kind of boring given the capabilities of WPF. Let’s see what we can do with style properties as the application is used.

    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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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