Leer en inglés

Compartir a través de


ContentControl.Content Propiedad

Definición

Obtiene o establece el contenido de ContentControl.

C#
[System.ComponentModel.Bindable(true)]
public object Content { get; set; }

Valor de propiedad

Objeto que contiene el contenido del control. El valor predeterminado es null.

Atributos

Ejemplos

En el ejemplo siguiente se muestra cómo crear cuatro Button controles con Content establecido en uno de los siguientes:

Nota

Aunque la versión de Lenguaje de marcado de aplicación extensible (XAML) del ejemplo podría usar las <Button.Content> etiquetas alrededor del contenido de cada botón, no es necesario. Para obtener más información, consulta Información general sobre XAML (WPF).

XAML
<!--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>
C#
// 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;

En la ilustración siguiente se muestran los cuatro botones creados en el ejemplo anterior.

Cuatro botones

Comentarios

Dado que la Content propiedad es de tipo Object, no hay restricciones en lo que se puede colocar en .ContentControl Content el objeto se muestra mediante , ContentPresenterque se encuentra en el ControlTemplate objeto de ContentControl. Cada ContentControl tipo de WPF tiene en ContentPresenter su valor predeterminado ControlTemplate. Para obtener más información sobre cómo muestra ContentPresenterContent, vea ContentPresenter.

Información sobre propiedades de dependencia

Campo identificador ContentProperty
Propiedades de metadatos establecidas en true Ninguno

Uso de atributos XAML

<object Content="content"/>  

Uso de elementos de propiedad XAML

<object> content</object>  

Valores XAML

Content
Texto o un único objeto.

Se aplica a

Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9