This article applies to applications being built using XAML for WPF, Windows Store App, or Windows Phone apps.
Each XAML object element is capable of displaying different content types. XAML provides a special property called Content that works to display the content of the element depending on the element capabilities. For example, a Content property of a Button can be a set to a string, an object, a UIElement or even and container. However, the Content property of a ListBox is set using the Items property.

Note: Some XAML object elements may not have the Content property available directly. It must be set through a property.

The code snippet in Listing 1 creates a Button control and sets its Content property to a string “Hello XAML”. The output looks as in Figure 1.

  1. <Button Height="50" Margin="10,10,350,310" Content="Hello XAML" />

Listing 1

Listing 2 is an alternative way to set the Content property of a Button.

  1. <Button Height="50" Margin="10,10,350,310">Hello XAML</Button>

Listing 2

The output of Listing 2 looks the same as in Figure 1.


Figure 1

A Button element can display other child elements as its content. The code listed in Listing 3 sets a Rectangle element as the content of the Button.

  1. <Button Height="80" Margin="10,80,300,170">
  2. <Rectangle Height="60" Width="120" Fill="Green"/>
  3. </Button>

Listing 3

The output of Listing 3 looks as in Figure 2.


Figure 2

Content property can also be a container or a parent element hosting child elements. The code listed in Listing 4 sets a StackPanel container with 5 child elements as the content of the Button.

  1. <Button Margin="10,201,100,40">
  2. <StackPanel Orientation="Horizontal">
  3. <Ellipse Height="60" Width="60" Fill="Red"/>
  4. <TextBlock TextAlignment="Center"><Run Text=" Red Circle"/></TextBlock>
  5. <TextBlock TextAlignment="Center"><Run Text=" "/></TextBlock>
  6. <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>
  7. <TextBlock TextAlignment="Center"><Run Text=" Green Rectangle"/></TextBlock>
  8. </StackPanel>
  9. </Button>

Listing 4

The output of Listing 4 looks as in Figure 3.


Figure 3

The final XAML code is listed in Listing 5.

  1. <Window x:Class="ContentPropertySample.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="400" Width="500" >
  5. <Grid x:Name="ParentGrid">
  6. <Button Height="50" Margin="10,10,350,310" Content="Hello XAML" />
  7. <Button Height="80" Margin="10,80,300,170">
  8. <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>
  9. </Button>
  10. <Button Margin="10,201,100,40">
  11. <StackPanel Orientation="Horizontal">
  12. <Ellipse Height="60" Width="60" Fill="Red"/>
  13. <TextBlock TextAlignment="Center"><Run Text=" Red Circle"/></TextBlock>
  14. <TextBlock TextAlignment="Center"><Run Text=" "/></TextBlock>
  15. <Rectangle Height="60" Width="120" Fill="Green"></Rectangle>
  16. <TextBlock TextAlignment="Center"><Run Text=" Green Rectangle"/></TextBlock>
  17. </StackPanel>
  18. </Button>
  19. </Grid>
  20. </Window>

Listing 5

The output of Listing 5 looks as in Figure 4.


Figure 4

As you can imagine from the preceding examples, you can pretty much host any user interfaces as content of a XAML element.

The code listed in Listing 6 creates the preceding Button controls dynamically in the code and sets their Content properties to a string, a Rectangle and a StackPanel respectively.

  1. // Button with string content
  2. Button helloButton = new Button();
  3. helloButton.Margin = new Thickness(10,10,350,310);
  4. helloButton.Content = "Hello XAML";
  5. // Button with a UIElement
  6. Button buttonWithRectangle = new Button();
  7. buttonWithRectangle.Height = 80;
  8. buttonWithRectangle.Margin = new Thickness(10, 80, 300, 170);
  9. // Create a Rectangle
  10. Rectangle greenRectangle = new Rectangle();
  11. greenRectangle.Height = 60;
  12. greenRectangle.Width = 120;
  13. greenRectangle.Fill = Brushes.Green;
  14. // Set Rectangle as Button.Content
  15. buttonWithRectangle.Content = greenRectangle;
  16. // Button with a Container, StackPanel
  17. Button buttonWithStackPanel = new Button();
  18. buttonWithStackPanel.Margin = new Thickness(10, 10, 350, 310);
  19. // Create a StackPanel and set its orinetation to horizontal
  20. StackPanel stackPanel = new StackPanel();
  21. stackPanel.Orientation = Orientation.Horizontal;
  22. // Create an Ellipse
  23. Ellipse redEllipse = new Ellipse();
  24. redEllipse.Width = 60;
  25. redEllipse.Height = 60;
  26. redEllipse.Fill = Brushes.Red;
  27. // Add to StackPanel
  28. stackPanel.Children.Add(redEllipse);
  29. // Create a TextBlock
  30. TextBlock textBlock1 = new TextBlock();
  31. textBlock1.TextAlignment = TextAlignment.Left;
  32. textBlock1.Text = "Red Circle";
  33. // Add to StackPanel
  34. stackPanel.Children.Add(textBlock1);
  35. // Create a TextBlock
  36. TextBlock space = new TextBlock();
  37. space.TextAlignment = TextAlignment.Center;
  38. space.Text = " ";
  39. // Add to StackPanel
  40. stackPanel.Children.Add(space);
  41. // Create a Rectangle
  42. Rectangle greenRectangle2 = new Rectangle();
  43. greenRectangle2.Height = 60;
  44. greenRectangle2.Width = 120;
  45. greenRectangle2.Fill = Brushes.Green;
  46. // Add to StackPanel
  47. stackPanel.Children.Add(greenRectangle2);
  48. // Create a TextBlock
  49. TextBlock textBlock2 = new TextBlock();
  50. textBlock2.TextAlignment = TextAlignment.Left;
  51. textBlock2.Text = "Green Rectangle";
  52. // Add to StackPanel
  53. stackPanel.Children.Add(textBlock2);
  54. // Set StackPaenl as Button.Content
  55. buttonWithStackPanel.Content = stackPanel;
  56. // Add dynamic button controls to the Window
  57. ParentGrid.Children.Add(helloButton);
  58. ParentGrid.Children.Add(buttonWithRectangle);
  59. ParentGrid.Children.Add(buttonWithStackPanel);

Listing 6

Summary

In this article, we saw the meaning of the Content property available to XAML elements and how to use it in our application.