XML
  Home arrow XML arrow Page 3 - 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 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

    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

    Styles and Control Templates - Reusing Styles


    (Page 3 of 4 )

    In addition to saving you from typing out the name of the class prefix for every property name, the TargetTypeattribute will also check that all classes that have the style applied are an instance of that type (or derived type). What that means is that if we leaveTargetTypeset toControl, we can apply it to aButtonelement, but not to aTextBlockelement, as the former derives ultimately fromControl but the latter does not.

    On the other hand, whileControlandTextBlockboth share the common ancestorFrameworkElement, theFrameworkElementclass doesn’t define aFontSizedependency property, so a style with aTargetTypeofFrameworkElementwon’t let us set theFontSizeproperty because its not there, despite the fact that bothControlandTextBlockhave aFontSizeproperty.

    Even with theTargetTypeset toControl, we gain a measure of reuse of our style across classes that derive fromControl—e.g.,Button,Label,Window, etc. However, if we drop theTargetTypeattribute from the style altogether, we gain a measure of reuse of styles across controls that don’t have a common base but share a dependency-property implementation. In my experimentation, I’ve found that dependency properties that share the same name across classes, such asControl.FontSizeandTextBlock.FontSize, also share an implementation. What that means is that even thoughControlandTextBlockeach define their ownFontSizeproperty, at runtime they share the implementation of this property, so I can write code like Example 5-8.

    Example 5-8.   Reusing a style between different element types

    ...
    <Style x:Key="CellTextStyle">
     
    <Setter Property="Control.FontSize" Value="32" />
    </Style>
    ...
    <!-- derives from Control -->
    <Button Style="{StaticResource CellTextStyle}" ... />

    <!-- does *not* derive from Control -->
    <TextBlock Style="{StaticResource CellTextStyle}" ... />
    ...

    In Example 5-8, I’ve dropped theTargetType attribute from the style definition, using instead the class prefix on each property the style sets. This style can be applied to aButtonelement, as you’d expect, but can also be applied to aTextBlock control, with theFontSizeset as specified by the style. The reason this works is that both theButton, which gets itsFontSizedependency property definition from the Control class, and theTextBlock, which provides it’s ownFontSizedependency property definition, share theFontSizedependency property implementation with theTextElementclass. Figure 5-2 shows the relationship of elements to their dependency-property implementations.

    As Figure 5-2 shows, if we wanted to, we could redefine our style in terms of theTextElementclass, even though it falls into the inheritance tree of neitherControlnorTextBlock, as in Example 5-9.


    Figure 5-2.  Elements and dependency properties

    Example 5-9. Depending on the implementation of dependency properties

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

    Taking this further, if we’d like to define a style that contains properties not shared by every element we apply them to, we can do that, too, as in Example 5-10.

    Example 5-10.   Styles can have properties that targets don’t have

    <Style x:Key="CellTextStyle">
      <Setter Property="TextElement.FontSize" Value="32" />
      <Setter Property="Button.IsCancel" Value="False" />
    </Style>
    ...
    <!-- has an IsCancel property --> <Button Style="{StaticResource CellTextStyle}" ... />

    <!-- does *not* have an IsCancel property -->
    <TextBlock Style="{StaticResource CellTextStyle}" ... />

    In Example 5-10, we’ve added theButton.IsCancelproperty to theCellTextStyle and applied it to theButtonelement, which has this property, and theTextBlockelement, which doesn’t. This is OK. At runtime, WPF will apply the dependency properties that exist on the elements that have them and silently swallow the ones that aren’t present.

    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