XML
  Home arrow XML arrow Page 4 - More on Triggers and Styles and Control Te...
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

More on Triggers and Styles and Control Templates
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2007-07-03

    Table of Contents:
  • More on Triggers and Styles and Control Templates
  • Control Templates
  • Control Templates and Styles
  • Content Presenters
  • The Real Work

  • 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

    Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!

    More on Triggers and Styles and Control Templates - Content Presenters


    (Page 4 of 5 )

    If you’ve ever driven by a billboard or a bench at a bus stop that says “Your advertisement here!” then that’s all you need to know to understand content presenters. A content presenter is the WPF equivalent of “your content here” that allows content held by a ContentContainer control to be plugged in at runtime.

    In our case, the content is the visualization of ourPlayerMoveobject. Instead of reproducing all of that work inside of the button’s new control template, we’d just like to drop it in at the right spot. The job of the content presenter is to take the content provided by the templated parent and do all of the things necessary to get it to show up properly, including styles, triggers, etc. The content presenter itself can be dropped into your template wherever you’d like to see it (including multiple times, if it tickles your fancy—e.g., to produce a drop shadow). In our case, we’ll compose a content presenter in Example 5-34 with the rectangle inside a grid using techniques from Chapter 2.

    Example 5-34.  A content presenter

    <Style TargetType="{x:Type Button}">
     
    <Setter Property="Background" Value="White" />
     
    ...
     
    <Setter Property="Template">
       
    <Setter.Value>
         
    <ControlTemplate>
           
    <Grid>
             
    <Rectangle Fill="{TemplateBinding Property=Background}" />
             
    <ContentPresenter 
               Content="{TemplateBinding Property=ContentControl.Content}" />

            </Grid>
         
    </ControlTemplate>
       
    </Setter.Value>
     
    </Setter>
     
    ...
    </Style>

    In Example 5-34, the content presenter’sContent property is bound to theContentControl.Contentproperty so that content comes through. As with styles, we can avoid prefixing template binding property names with classes by setting theTargetType attribute on theContentTemplate element:

      <ControlTemplate TargetType="{x:Type Button}">
       
    <Grid>
         
    <Rectangle Fill="{TemplateBinding Property=Background}" />
         
    <ContentPresenter
           
    Content="{TemplateBinding Property=Content}" />
        </Grid>
      </ControlTemplate>

    Further, with theTargetType property in place, you can drop the explicit template binding on theContent property altogether, as it’s now set automatically:

      <ControlTemplate TargetType="{x:Type Button}">
        <Grid>
          <Rectangle Fill="{TemplateBinding Property=Background}" />
         
    <!-- with TargetType set, the template binding for the -->
         
    <!-- Content property is no longer required --
    >
          <ContentPresenter />
        </Grid>
     
    </ControlTemplate>

    The content presenter is all we need to get our game back to being functional, as shown in Figure 5-19.


    Figure 5-19.  Adding a content presenter to our control template (Color Plate 14)

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