ContentPresenter Class
This page is specific to:.NET Framework Version:3.03.5Silverlight 34.0
.NET Framework Class Library
ContentPresenter Class

Updated: July 2008

Displays the content of a ContentControl.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

'Usage

Dim instance As ContentPresenter

'Declaration

<LocalizabilityAttribute(LocalizationCategory.None, Readability := Readability.Unreadable)> _
Public Class ContentPresenter _
    Inherits FrameworkElement
<ContentPresenter .../>
Remarks

Typically, when you would use the ContentPresenter directly is within the ControlTemplate of a ContentControl to mark where the content is to be added.

Every ContentControl type has a ContentPresenter within its ControlTemplate. The ContentPresenter is responsible for the UI generation. For example, when you apply a DataTemplate to an ItemsControl such as a ListBox, behind the scenes it is actually the ContentPresenter within the ListBoxItem that creates the UI of each item. For more information about that particular example, see ItemTemplate.

Examples

The following shows a Button Style that sets the ControlTemplate of a Button. The ControlTemplate defines that the Button appears as an Ellipse within a Grid element. The ContentPresenter marks that the Content of the Button should be displayed and centered vertically and horizontally within the Grid.

<Style TargetType="Button">
  <!--Set to true to not get any properties from the themes.-->
  <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Ellipse Fill="{TemplateBinding Background}"/>
          <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

The following image shows the appearance of the Button when this gets applied:

Button ControlTemplate sample
Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.ContentPresenter
              System.Windows.Controls..::.ScrollContentPresenter
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Change History

Date

History

Reason

July 2008

Added new members: ContentStringFormat property, ContentStringFormatProperty field, OnContentStringFormatChanged method.

SP1 feature change.

Community Content

Many ways to skin a control
Added by:LukeSkywalker

In the parts and states model, a Silverlight paradigm coming to WPF, a specific named part is sometimes needed to mark the bit where a controls content goes. For example, the template for a TextBox requires an element named as such x:Name="ContentElement" otherwise the TextBox has no text, and its just a box.

In WPF and in some other Silverlight controls, <ContentPresenter /> is all that is needed in the template for the magic to happen.

But in an example I have seen, the ContentPresenter.Content property is hooked up using TemplateBinding as in the example below, however it appears to work without it and...

...actually any type of ContentControl, Label etc. seems to work as long as the Content property is template bound.

This chunk of XAML should go somewhere in your ControlTemplate:

<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View