#971 – Items Property is a Content Property

Every control that derives from ItemsControl (including the ListBox) has an Items property that stores the collection of items represented by the control.

The Items property of an ItemsControl is also its content property.  This means that you can set the value of the Items property directly in XAML by specifying a list of child elements for the control that derives from ItemsControl.

For example, you can explicitly specify the child elements using property element syntax:

        <ListBox Margin="15" Width="250" Height="250">
            <ListBox.Items>
                <ListBoxItem Content="Thing 1"/>
                <ListBoxItem Content="Thing 2"/>
            </ListBox.Items>
        </ListBox>

Because the Items property is a content property, you can be more concise, doing the following:

        <ListBox Margin="15" Width="250" Height="250">
            <ListBoxItem Content="Thing 1"/>
            <ListBoxItem Content="Thing 2"/>
        </ListBox>
Advertisement

#846 – Including an Underscore Character in a Label

When you include an underline ‘_’ character in the Content property of a Label, the underline character will be used to determine the access key for the label.  If you want to actually have a underline character show up in your user interface, you need to use a double underline ‘__’ sequence for the underline character.

    <StackPanel Orientation="Horizontal">
        <Label Content="Enter Your Super__Cool _Nickname"
               Target="{Binding ElementName=txtName}"
               VerticalAlignment="Center"/>
        <TextBox Name="txtName" Width="150"
                 VerticalAlignment="Center"/>
    </StackPanel>

846-001

#836 – Setting a ContentControl’s Content to a CLR Object

You’ll typically set the Content property of a content control to an instance of an UIElement, which will typically include one or more controls.  (E.g. On a Button).

You can also set a Content property to a plain CLR object, i.e. an object that derives from System.Object.  When you do this, a content control will render the object by calling its ToString method.

In the example below, a Tooltip’s content is set to an instance of a Dog class.  When the tooltip is displayed, the dog’s ToString method is called.

<Window.Resources>
    <local:Dog x:Key="myDog" Name="Kirby" Age="15" FavToy="Tennis ball"/>
</Window.Resources>

<StackPanel DataContext="{StaticResource myDog}">
    <Label Content="{Binding Name}" Margin="10"
           ToolTip="{Binding}"/>
</StackPanel>

Assume that the ToString method dumps out the contents of the object:

        public override string ToString()
        {
            StringBuilder sbValue = new StringBuilder(string.Format("Dog {0}:\n", Name));
            sbValue.AppendFormat("  Age: {0}\n", Age);
            sbValue.AppendFormat("  Favorite Toy: {0}\n", FavToy);

            return sbValue.ToString();
        }

836-001

#300 – Button is a ContentControl

Button is a ContentControl, which means that it can contain exactly one child control.  The child element can be any .NET object.

A simple string is the most common content for a Button.

        <Button Content="Click me" Click="Button_Click"/>

Examining the Button control at runtime, you can see that its stores an instance of a string in its Content property.  Also note that the base type of Content is object, meaning that the button could store any .NET object as its content.

Here’s a Button that contains a single Ellipse control as its content:

        <Button Click="Button_Click" Width="140" Height="80">
            <Ellipse Fill="PaleGreen" Stroke="Blue" StrokeThickness="5" Width="120" Height="60"/>
        </Button>


And here’s a Button that uses a StackPanel to host several child controls.

        <Button Click="Button_Click" Width="140" Height="120">
            <StackPanel>
                <Ellipse Stroke="Blue" StrokeThickness="5" Width="120" Height="60"/>
                <Label Content="Name my ellipse"/>
                <TextBox />
            </StackPanel>
        </Button>