Partilhar via


UserControl.Content Property

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

Gets or sets the content that is contained within a user control.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Content As UIElement
public UIElement Content { get; set; }
<UserControl ...>
  singleContentElement
</UserControl>
-or-
<UserControl .../>

XAML Values

  • singleContentElement
    Exactly one object element for a class that derives from UIElement. This is almost always an object that can take child elements (such as a Panel) so that multiple elements can be added to content. XAML processing of a UserControl has a special behavior that sets this content to the otherwise protected Content property.

Property Value

Type: System.Windows.UIElement
The content of the user control.

Remarks

Dependency property identifier field: ContentProperty

The XAML usage shown is special parsing handling of the otherwise protected Content property. When the partial classes of the UserControl subclass you define that serves as a page root are joined, the XAML content of the UserControl object element in page markup becomes the Content value of your page class, and the content of the loaded page in the application.

Examples

This following example creates a UserControl called NameReporter that asks for a name of a person, and reports it back to the user. The Content of NameReporter is a StackPanel. The TextBlock controls, TextBox controls, and Button are contained in the Children property of the StackPanel. For the complete example, see the UserControl class reference.

<UserControl x:Class="UserControlExample.NameReporter"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >
  <StackPanel HorizontalAlignment="Center">

    <StackPanel.Resources>
      <!--Create a Style for a TextBlock.-->
      <Style TargetType="TextBlock" x:Key="TextBlockStyle">
        <Setter Property="Foreground" Value="Navy"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
      </Style>

      <!--Create a Style for a TextBlock.-->
      <Style TargetType="TextBox" x:Key="TextBoxStyle">
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="Margin" Value="4"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Background" Value="Blue"/>
      </Style>
    </StackPanel.Resources>

    <TextBlock FontSize="18" Text="Enter your name."/>
    <StackPanel Orientation="Horizontal">
      <TextBlock Style="{StaticResource TextBlockStyle}">
                First Name:
      </TextBlock>
      <TextBox Name="firstName" Style="{StaticResource TextBoxStyle}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <TextBlock Style="{StaticResource TextBlockStyle}">
                Last Name:
      </TextBlock>
      <TextBox Name="lastName" Style="{StaticResource TextBoxStyle}"  
                     Margin="6,4,4,4"/>
    </StackPanel>
    <Button Width="50" Content="Submit" Click="Button_Click"/>

  </StackPanel>
</UserControl>

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.