XML
  Home arrow XML arrow Page 2 - Styles and Control Templates
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 
Moblin 
JMSL Numerical Library 
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

Styles and Control Templates
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-06-21

    Table of Contents:
  • Styles and Control Templates
  • Inline Styles
  • Reusing Styles
  • Named 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


    Styles and Control Templates - Inline Styles


    (Page 2 of 4 )

    Each “style-able” element in WPF has a Style property, which can be set inline using standard XAML property-element syntax (discussed in Chapter 1), as in Example 5-4.

    Example 5-4.   Setting an inline style

    <Button ... x:Name="cell00" /> 
      <Button.Style>
       
    <Style> 
         
    <Setter Property="Button.FontSize" Value="32" />
         
    <Setter Property="Button.FontWeight" Value="Bold" />
       
    </Style
    >
      </Button.Style>
    </Button>

    Because we want to bundle two property values into our style, we have a Style element with twoSetter sub-elements, one for each property we want to set—i.e.,FontSizeandFontWeight—both with theButtonprefix to indicate the class that contains the property. Properties suitable for styling are dependency properties, which are described in Chapter 9.

    Due to the extra style syntax and because inline styles can’t be shared across elements, inline styles actually involve more typing than just setting the properties. For this reason, inline styles aren’t used nearly as often as named styles.

    Named Styles

    By hoisting the same inline style into a resource (as introduced in Chapter 1), we can award it a name and use it by name in our button instances, as shown in Example 5-5.

    Example 5-5.   Setting a named style

    <!-- Window1.xaml -->
    <Window ...>
      <Window.Resources>
       
    <Style x:Key="CellTextStyle">
         
    <Setter Property="Control.FontSize" Value="32" />
         
    <Setter Property="Control.FontWeight" Value="Bold" />
       
    </Style>
      </Window.Resources>
      ...
     
    <Button Style="{StaticResource CellTextStyle}" ... x:Name="cell00" />
      ...
    </Window>

    In Example 5-5, we’ve used theControlprefix on our properties instead of theButtonprefix to allow the style to be used more broadly, as we’ll soon see.

    The TargetType Attribute

    As a convenience, if all of the properties can be set on a shared class, like Control in our example, we can promote the class prefix into theTargetType attribute and remove it from the name of the property, as in Example 5-6.

    Example 5-6.   A target-typed style

    <Style x:Key="CellTextStyle" TargetType="{x:Type Control}">
     
    <Setter Property="FontSize" Value="32" />
     
    <Setter Property="FontWeight" Value="Bold" />
    </Style>

    When providing aTargetTypeattribute, you can only set properties available on that type. If you’d like to expand to a greater set of properties down the inheritance tree, you can do so by using a more derived type, as in Example 5-7.

    Example 5-7.   A more derived target-typed style

    <Style x:Key="CellTextStyle" TargetType="{x:Type Button}">
     
    <!-- IsCancel is a Button-specific property -->
     
    <Setter Property="IsCancel" Value="False" />
     
    <Setter Property="FontSize" Value="32" />
     
    <Setter Property="FontWeight" Value="Bold" />
    </Style>

    In this case, theIsCancelproperty is only available onButton, so to set it, we need to switch theTargetTypeattribute for the style.

    You may be wondering why I’m setting theFontSizeto"32"instead of"32pt"when the latter is more in line with how font sizes are specified and the two representations are definitely not equivalent (the former is pixels, while the latter is points). I’m using pixels because as of this writing, WPF styles using a non-prefixed property allow"32pt"to be specified forFontSize, while prefixed properties do not. For example, the following works (assuming aTargetTypeis set):

      <Setter Property="FontSize" Value="32pt" />

    whereas the following does not (regardless of whether aTargetTypeis set or not):

      <Setter Property="Control.FontSize" Value="32pt" />

    Hopefully this problem will have been fixed by the time you read this (and not replaced with others).

    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 3 hosted by Hostway