ContentControl.Content ContentControl.Content ContentControl.Content ContentControl.Content Property

定义

获取或设置 ContentControl 的内容。Gets or sets the content of a ContentControl.

public:
 property System::Object ^ Content { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Bindable(true)]
public object Content { get; set; }
member this.Content : obj with get, set
Public Property Content As Object

属性值

一个包含控件内容的对象。An object that contains the control's content. 默认值为 nullThe default value is null.

示例

下面的示例演示如何创建四个Button控件, Content并将设置为以下其中一项:The following example demonstrates how to create four Button controls with Content set to one of the following:

备注

尽管该示例的<Button.Content> 版本可以使用每个按钮的内容周围的标记,但这并不是必需的。可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)Although the 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) version of the example could use the <Button.Content> tags around the content of each button, it is not necessary. 有关详细信息, 请参阅XAML 概述 (WPF)For more information, see XAML Overview (WPF).

<!--Create a Button with a string as its content.-->
<Button>This is string content of a Button</Button>

<!--Create a Button with a DateTime object as its content.-->
<Button xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <sys:DateTime>2004/3/4 13:6:55</sys:DateTime>
</Button>

<!--Create a Button with a single UIElement as its content.-->
<Button>
  <Rectangle Height="40" Width="40" Fill="Blue"/>
</Button>

<!--Create a Button with a panel that contains multiple objects 
as its content.-->
<Button>
  <StackPanel>
    <Ellipse Height="40" Width="40" Fill="Blue"/>
    <TextBlock TextAlignment="Center">Button</TextBlock>
  </StackPanel>
</Button>
// Create a Button with a string as its content.
Button stringContent = new Button();
stringContent.Content = "This is string content of a Button";

// Create a Button with a DateTime object as its content.
Button objectContent = new Button();
DateTime dateTime1 = new DateTime(2004, 3, 4, 13, 6, 55);

objectContent.Content = dateTime1;

// Create a Button with a single UIElement as its content.
Button uiElementContent = new Button();

Rectangle rect1 = new Rectangle();
rect1.Width = 40;
rect1.Height = 40;
rect1.Fill = Brushes.Blue;
uiElementContent.Content = rect1;

// Create a Button with a panel that contains multiple objects 
// as its content.
Button panelContent = new Button();
StackPanel stackPanel1 = new StackPanel();
Ellipse ellipse1 = new Ellipse();
TextBlock textBlock1 = new TextBlock();

ellipse1.Width = 40;
ellipse1.Height = 40;
ellipse1.Fill = Brushes.Blue;

textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.Text = "Button";

stackPanel1.Children.Add(ellipse1);
stackPanel1.Children.Add(textBlock1);

panelContent.Content = stackPanel1;
' Add a string to a button.
Dim stringContent As New Button()
stringContent.Content = "This is string content of a Button"

' Add a DateTime object to a button.
Dim objectContent As New Button()
Dim dateTime1 As New DateTime(2004, 3, 4, 13, 6, 55)

objectContent.Content = dateTime1

' Add a single UIElement to a button.
Dim uiElementContent As New Button()

Dim rect1 As New Rectangle()
rect1.Width = 40
rect1.Height = 40
rect1.Fill = Brushes.Blue
uiElementContent.Content = rect1

' Add a panel that contains multpile objects to a button.
Dim panelContent As New Button()
Dim stackPanel1 As New StackPanel()
Dim ellipse1 As New Ellipse()
Dim textBlock1 As New TextBlock()

ellipse1.Width = 40
ellipse1.Height = 40
ellipse1.Fill = Brushes.Blue

textBlock1.TextAlignment = TextAlignment.Center
textBlock1.Text = "Button"

stackPanel1.Children.Add(ellipse1)
stackPanel1.Children.Add(textBlock1)

panelContent.Content = stackPanel1

下图显示了在上一个示例中创建的四个按钮。The following illustration shows the four buttons created in the previous example.

四个按钮Four buttons

注解

由于属性的类型Object为, 因此不会限制ContentControl你可以放入的内容。 ContentBecause the Content property is of type Object, there are no restrictions on what you can put in a ContentControl. 由显示,它ContentControl位于的中。 ControlTemplate Content ContentPresenterThe Content is displayed by a ContentPresenter, which is in the ControlTemplate of the ContentControl. ContentControl ContentPresenter ControlTemplate的每个类型的默认值均为。 WPFWPFEvery ContentControl type in WPFWPF has a ContentPresenter in its default ControlTemplate. 有关显示器的ContentPresenter显示Content方式的详细信息, ContentPresenter请参阅。For more information about how the ContentPresenter displays Content, see ContentPresenter.

依赖项属性信息Dependency Property Information

标识符字段Identifier field ContentProperty
元数据属性设置为trueMetadata properties set to true NoneNone

XAML 属性用法XAML Attribute Usage

<object Content="content"/>  

XAML 属性元素用法XAML Property Element Usage

<object> content</object>  

XAML 值XAML Values

Content
文本或单个对象。Text or a single object.

适用于