#80 – Content Properties and Collection Syntax Express UIElement Containment

Using both a content property and the collection syntax is quite common in XAML and often expresses containment of a collection of UIElement objects in a parent container.

For example, the StackPanel is a parent control, containing a number of child controls:

 <StackPanel>
     <Label Content="A label" Height="28" />
     <Button Content="Click Me" Height="23" />
 </StackPanel>

In this example, containment is expressed by listing the child controls contained in the StackPanel as child elements in XAML.

This containment shorthand is possible for two reasons:

  • StackPanel‘s content property is Children, an UIElementCollection
  • We can specify the members of the UIElementCollection using collection syntax, by listing its members

All containers that inherit from System.Windows.Controls.Panel have Children as their content property–including Grid, StackPanel, Canvas, WrapPanel, etc.

Advertisement

#79 – Content Properties for Common WPF Types

Here is a list of the content properties for some common WPF types.  You can set values for these properties in XAML using one or more child elements directly, rather than having to use the property element syntax.

E.g. Because Text is the content property for a TextBox, you can specify a value for Text as a child element of the TextBox element:

 <TextBox Height="23" Width="120">
     Some text...
 </TextBox>

Here is the list of some common WPF types and their content properties:

  • Button – Content
  • CheckBox – Content
  • ComboBox – Items
  • Grid – Children
  • GridView – Columns
  • GroupBox – Content
  • Label – Content
  • ListBox – Items
  • ListView – Items
  • Page – Content
  • Panel – Children
  • StackPanel – Children
  • TextBox – Text
  • TreeView – Items
  • Window – Content

For a more complete list, see Building A List of Types That Have Content Properties.