Content​Control Class

Definition

Represents a control with a single piece of content of any type.

[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
[System.Windows.Markup.ContentProperty("Content")]
public class ContentControl : System.Windows.Controls.Control, System.Windows.Markup.IAddChild
Inheritance
Derived
Attributes
Implements

Inherited Members

System.Object

System.Windows.Controls.Control

System.Windows.DependencyObject

System.Windows.FrameworkElement

System.Windows.Media.Visual

System.Windows.Threading.DispatcherObject

System.Windows.UIElement

Examples

The following example demonstrates how to create the four buttons shown in the Remarks section.

Note

Although the 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. 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 example shows how to make a style for a ContentControl so that the control has an enhanced visual appearance.

<Style x:Key="ContentCtrl" TargetType="{x:Type ContentControl}">
  <Setter Property="Background" Value="Red"/>
  <Setter Property="Foreground" Value="Green"/>
  <Setter Property="FontSize" Value="20"/>
  <Setter Property="FontWeight" Value="Bold"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ContentControl}">
        <Grid>
          <!--Keep the Ellipse a circle when ContentControl.Width
              is set.-->
          <Ellipse Width="{TemplateBinding Width}"
                   Height="{TemplateBinding Width}"
                   Fill="{TemplateBinding Background}"/>
            <ContentPresenter VerticalAlignment="Center"
                              HorizontalAlignment="Center"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
<ContentControl Width="75" Style="{StaticResource ContentCtrl}" 
                Content="Hello"/>

Remarks

The ContentControl can contain any type of common language runtime object (such as a string or a DateTime object) or a UIElement object (such as a Rectangle or a Panel). This enables you to add rich content to controls such as Button and CheckBox.

The following illustration shows four buttons whose Content property is set to one of the following:

Four buttons
Four buttons with different types of content

AContentControl has a limited default style. If you want to enhance the appearance of the control, you can create a new DataTemplate. For more information, see Data Templating Overview. Another typical scenario is to use the ContentControl to show more information about an item selected in an ItemsControl control. For more information, see How to: Bind to a Collection and Display Information Based on Selection.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Default WPF Themes.

Constructors

Content​Control()

Initializes a new instance of the ContentControl class.

Fields

Content​Property

Identifies the Content dependency property.

Content​String​Format​Property

Identifies the ContentStringFormat dependency property.

Content​Template​Property

Identifies the ContentTemplate dependency property.

Content​Template​Selector​Property

Identifies the ContentTemplateSelector dependency property.

Has​Content​Property

Identifies the HasContent dependency property.

Properties

Content

Gets or sets the content of a ContentControl.

Content​String​Format

Gets or sets a composite string that specifies how to format the Content property if it is displayed as a string.

Content​Template

Gets or sets the data template used to display the content of the ContentControl.

Content​Template​Selector

Gets or sets a template selector that enables an application writer to provide custom template-selection logic.

Has​Content

Gets a value that indicates whether the ContentControl contains content.

Logical​Children

Gets an enumerator to the content control's logical child elements.

Methods

Add​Child(​Object)

Adds a specified object as the child of a ContentControl.

Add​Text(​String)

Adds a specified text string to a ContentControl.

On​Content​Changed(​Object, ​Object)

Called when the Content property changes.

On​Content​String​Format​Changed(​String, ​String)

Occurs when the ContentStringFormat property changes.

On​Content​Template​Changed(​Data​Template, ​Data​Template)

Called when the ContentTemplate property changes.

On​Content​Template​Selector​Changed(​Data​Template​Selector, ​Data​Template​Selector)

Called when the ContentTemplateSelector property changes.

Should​Serialize​Content()

Indicates whether the Content property should be persisted.

Explicit Interface Implementations

I​Add​Child.​Add​Child(​Object)

This type or member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

I​Add​Child.​Add​Text(​String)

This type or member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.