XML
  Home arrow XML arrow Page 2 - A Closer Look at Styles and Control Templa...
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 
VeriSign Whitepapers 
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

A Closer Look at Styles and Control Templates
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-06-27

    Table of Contents:
  • A Closer Look at Styles and Control Templates
  • Setting Styles Programmatically
  • Element-Typed Styles
  • Data Templates and Styles

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    A Closer Look at Styles and Control Templates - Setting Styles Programmatically


    (Page 2 of 4 )

    Once a style has a name, it’s easily available from our code. For example, we might decide that we’d like each player to have their own style. In this case, using named styles in XAML at compile time won’t do the trick, since we want to set the style based on the content, which isn’t known until runtime. However, there’s nothing that requires us to set the Style property of a control statically; we can set it programmatically as well, as we do in Example 5-14.

    Example 5-14.   Setting styles programmatically

    public partial class Window1 : Window {
     
    void cell_Click(object sender, RoutedEventArgs e) {
        Button button = (Button)sender;
        ...
        // Set button content
        button.Content = this.CurrentPlayer;
        ...
        if( this.CurrentPlayer == "X" ) {
          button.Style = (Style)FindResource("XStyle");
          this.CurrentPlayer == "O";
        }
        else { 
          
    button.Style = (Style)FindResource("OStyle");
          this.CurrentPlayer == "X";
        }
        ... 
      
    }
      ...
    }

    In Example 5-14, whenever the player clicks, in addition to setting the button’s content, we pull a named style out of the window’s resources and use that to set the button’s style. This assumes a pair of named styles defined in the window’s scope, as in Example 5-15.

    Example 5-15.   Styles pulled out via FindResource

    <Window.Resources>
      <Style x:Key="CellTextStyle">
        <Setter Property="TextElement.FontSize" Value="32" />
        <Setter Property="TextElement.FontWeight" Value="Bold" />
      </Style>
     
    <Style x:Key="XStyle" BasedOn="{StaticResource CellTextStyle}">
       
    <Setter Property="TextElement.Foreground" Value="Red" />
      </Style>
      <Style x:Key="OStyle" BasedOn="{StaticResource CellTextStyle}">

       
    <Setter Property="TextElement.Foreground" Value="Green" />
      </Style>

    </Window.Resources>  

    With these styles in place and the code to set the button style along with content, we get Figure 5-5.

    Notice that the Xs and Os are colored according to the named player styles. In this particular case (and in many other cases, too), data triggers (discussed in “Data Triggers,” later in this chapter) should be preferred to setting styles programmatically, but you never know when you’re going to have to jam.

    As with all XAML constructs, you are free to create styles themselves programmatically. Appendix A is a good introduction on how to think about going back and forth between XAML and code.

     


    Figure 5-5.  Setting styles programmatically based on an object's content (Color Plate 9)

    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





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