共用方式為


ContentPresenter.Content Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Property Content As Object
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 nulla null reference (Nothing in Visual Basic).

Remarks

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.

Examples

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

Run this sample

<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>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.