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

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

    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!

    A Closer Look at Styles and Control Templates - Element-Typed Styles


    (Page 3 of 4 )

    Named styles are useful when you’ve got a set of properties to be applied to specific elements. However, if you’d like to apply a style uniformly to all instances of a certain type of element, set the TargetType without a Key, as in Example 5-16.

    Key , as in Example 5-16.TargetTypewithout a Key , as in Example 5-16.Named styles are useful when you’ve got a set of properties to be applied to specific elements. However, if you’d like to apply a style uniformly to all instances of a certain type of element, set the TargetType without a Key, as in Example 5-16.

    Example 5-16.   Element-typed styles

    ...
    <!-- no Key -->
    <Style TargetType="{x:Type Button}">
      <Setter Property="FontSize" Value="32" />
     
    <Setter Property="FontWeight" Value="Bold" />
    </Style>
    <!-- no Key -->
    <Style
    TargetType="{x:Type TextBlock}">
      <Setter Property="FontSize" Value="32" />
     
    <Setter Property="FontWeight" Value="Thin" />
     
    <Setter Property="Foreground" Value="White" />
     
    <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
    ...
    <Button Grid.Row="0" Grid.Column="0" x:ID="cell00" />
    ...
    <TextBlock Grid.Row="5" Grid.ColumnSpan="5" x:ID="statusTextBlock" />
    ...

    In Example 5-16, we’ve got two styles, one with aTargetTypeofButton and noKey and another with aTargetType ofTextBlockand noKey. Both work in the same way; when an instance ofButtonorTextBlockis created without an explicitStyle attribute setting, it uses the style that matches the target type of the style to the type of the control. Our element-typed styles return our game to looking like Figure 5-4 again.

    Element-typed styles are handy whenever you’d like all instances of a certain element to share a look, depending on the scope. For example, we’ve scoped the styles in our sample thus far at the top-level Window in Example 5-17.

    Example 5-17.   Styles scoped to the Window

    <!-- Window1.xaml -->
    <Window ...>
     
    <!-- every Button or TextBlock in the Window is affected -->
      <Window.Resources>
       
    <Style TargetType="{x:Type Button}">...</Style>
       
    <Style TargetType="{x:Type TextBlock}">...</Style>
      </Window.Resources>
      ...
    </Window>

    However, you may want to reduce the scope of an element-typed style. In our sample, it would work just as well to scope the styles inside the grid so that only buttons and text blocks in the grid are affected, as in Example 5-18.

    Example 5-18.   Styles scoped below the Window

    <!-- Window1.xaml -->
    <Window ...>
     
    <Grid ...>
       
    <!-- only Buttons or TextBlocks in the Grid are affected --> 
        <Grid.Resources>
         
    <Style TargetType="{x:Type Button}">...</Style>
         
    <Style TargetType="{x:Type TextBlock}">... </Style>
        </Grid.Resources>
       
    ...
      </Grid>
     
    <!-- Buttons and TextBlocks outside the Grid are unaffected -->
      ...
    </Window>

    Or, if you want to make your styles have greater reach in your project, you can put them into the application scope, as in Example 5-19.

    Example 5-19.   Styles scoped to the application

    <!-- MyApp.xaml -->
    <Application ...>
     
    <!-- every Button or TextBlock in the Application is affected -->
      <Application.Resources>
       
    <Style TargetType="{x:Type Button}">...</Style>
       
    <Style TargetType="{x:Type TextBlock}">...</Style> 
      </Application.Resources>
    </Application>

    In general it’s useful to understand the scoping rules of element-typed styles so you can judge their effect on the various pieces of your WPF object model. Chapter 6 discusses resource scoping of all kinds, including styles, in more detail.

    Named versus element-typed styles

    When choosing between styles set by style name or by element type, one of our reviewers said that in his experience, once you get beyond 10 styles based on element type, it was too hard to keep track of where a particular control was getting its style from. This is one reason that I’m a big fan of named styles.

    To me, a style is a semantic tag that will be applied to content in one place and awarded a visual representation in another. As simple as our tic-tac-toe sample is, we’ve already got two styles, one for the status text and one for the move cell; before we’re done, we’re going to have more. The major differentiating factor is going to be the kind of data we’ll be showing in these elements, not the type of the element holding the data. In fact, we’ll have several styles assigned toTextBoxcontrols, which negates the use of type-based styles anyway, even for this simple application.

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