XML
  Home arrow XML arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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


    A Closer Look at Styles and Control Templates


    (Page 1 of 4 )

    Picking up from where we left off last week, you'll learn how to override style properties, set styles programmatically, and more. 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.

    Overriding Style Properties

    Further, if we want to override a style property on a specific instance, we can do so by setting the property on the instance, as in Example 5-12.

    Example 5-12.   Overriding the FontWeight property from the style

    <Style x:Key="CellTextStyle">
      <Setter Property="TextElement.FontSize" Value="32" />
     
    <Setter Property="TextElement.FontWeight" Value="Bold" />
    </Style>
    ...
    <TextBlock
     
    Style="{StaticResource CellTextStyle}"
      FontWeight="Thin" ... />

    In Example 5-12, the TextBlock instance property setting of FontWeight take precedence over the style property settings of FontWeight.

    Inheriting Style Properties

    To complete the object-oriented triumvirate of reuse, override, and inheritance, you can inherit a style from a base style, adding new properties or overriding existing ones, as in Example 5-13.

    Example 5-13.  Style inheritance

    <Style x:Key="CellTextStyle">
      <Setter Property="TextElement.FontSize" Value="32" />
      <Setter Property="TextElement.FontWeight" Value="Bold" />
    </Style>
    <Style x:Key="StatusTextStyle" BasedOn="{StaticResource CellTextStyle}">

      <Setter Property="TextElement.FontWeight" Value="Thin" />
     
    <Setter Property="TextElement.Foreground" Value="White" />
     
    <Setter Property="TextBlock.HorizontalAlignment" Value="Center" />
    </Style>

    TheBasedOnstyle attribute is used to designate the base style. In Example 5-13, theStatusTextStyle style inherits all of theCellTextStyleproperty setters, overrides theFontWeight, and adds setters forForeground andHorizontalAlignment. Notice that theHorizontalAlignmentproperty uses aTextBlockprefix; this is becauseTextElementdoesn’t have aHorizontalAlignmentproperty.

    Our current use of styles causes our tic-tac-toe game to look like Figure 5-4.


    Figure 5-4.  A tic-tac-toe game with more style

    Our application so far is pretty good, especially with the thin font weight on the status text, but we can do better.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek