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

UserControl.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 content that is contained within a user control.

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

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.

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.

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.


<phone:PhoneApplicationPage x:Class="UserControlExample.NameReporter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="800" d:DesignWidth="480">
  <StackPanel HorizontalAlignment="Center">

    <StackPanel.Resources>
      <!--Create a Style for a TextBlock.-->
      <Style TargetType="TextBlock" x:Key="TextBlockStyle">
                <Setter Property="Foreground" Value="{StaticResource PhoneForegroundColor}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeSmall}"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Margin" Value="4"/>
            </Style>

      <!--Create a Style for a TextBlock.-->
      <Style TargetType="TextBox" x:Key="TextBoxStyle">
        <Setter Property="Width" Value="200"/>
                <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeSmall}"/>
                <Setter Property="Background" Value="{StaticResource PhoneForegroundColor}"/>
      </Style>
    </StackPanel.Resources>

        <TextBlock FontSize="{StaticResource PhoneFontSizeSmall}" Text="Enter your name"/>
    <StackPanel Orientation="Horizontal">
           <TextBlock Style="{StaticResource TextBlockStyle}" Text="First Name:" />
           <TextBox Name="firstName" Style="{StaticResource TextBoxStyle}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Last Name:" />

            <TextBox Name="lastName" Style="{StaticResource TextBoxStyle}"   />
    </StackPanel>
    <Button  Content="Submit" Click="Button_Click"/>

  </StackPanel>
</phone:PhoneApplicationPage>


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft