Content Property
Collapse the table of content
Expand the table of content

ContentPresenter.Content Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets or sets the data that is used to generate the child elements of a ContentPresenter.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public Object Content { get; set; }
<ContentPresenter Content="{Binding}"/>

Property Value

Type: System.Object
The data that is used to generate the child elements. The default is null.

Dependency property identifier field: ContentProperty

When you put a ContentPresenter in the ControlTemplate of a ContentControl, it automatically displays the Content of the templated control. For example, if you put a ContentPresenter in a ControlTemplate of a Button, the Content property of the ContentPresenter is implicitly bound to the Content of the Button that uses the ControlTemplate.

The XAML usage shown is the necessary when a ContentPresenter is part of a DataTemplate. When a ContentPresenter is part of a ControlTemplate, you do not need to template bind the Content property.

The following example creates a ControlTemplate for Button and uses a ContentPresenter to display the content of the Button.


<StackPanel>
  <StackPanel.Resources>
    <Style x:Key="newButtonTemplate" TargetType="Button">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="Button">
            <Grid>
              <Rectangle Fill="{TemplateBinding Background}" />
              <ContentPresenter 
                HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                VerticalAlignment="{TemplateBinding VerticalAlignment}" />
            </Grid>
          </ControlTemplate>

        </Setter.Value>
      </Setter>
    </Style>
  </StackPanel.Resources>

  <TextBlock Text="ContentPresenter Demonstration" Margin="0,20,10,20"
           FontFamily="Verdana" FontSize="18" FontWeight="Bold"
           Foreground="#FF5C9AC9" Grid.Row="0" />

  <Button Content="default button" 
          Width="200" Margin="7"
          Click="Button_Click"/>

  <Button Content="button with control template" 
          Style="{StaticResource newButtonTemplate}"
          Margin="7" Background="LightGray" Width="200"
          Click="Button_Click"/>

  <Button Content="another button with control template" 
          Style="{StaticResource newButtonTemplate}"
          Margin="7" Background="LightBlue" Width="200" 
          Click="Button_Click"/>

</StackPanel>


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2016 Microsoft