继承自ContentControl类型的控件可以包含任意内容,它的Content属性,是一个Object类型,典型的控件有(Button、ButtonBase、CheckBox、ComboBoxItem、ContentControl、Frame、GridViewColumnHeader、GroupItem、Label、ListBoxItem、ListViewItem、NavigationWindow、RadioButton、RepeatButton、ScrollViewer、StatusBarItem、ToggleButton、ToolTip、UserControl、Window)下面Content设置为字符串、DateTime 对象、Rectangle 和包含 Ellipse
<Window x:Class="Example_13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Example_13"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Margin="10,10,10,10">
<Button.Content>
<sys:String>包含一个字符串内容的Button</sys:String>
</Button.Content>
</Button>
<Button Grid.Row="1" Margin="10,10,10,10">
<Button.Content>
<sys:DateTime>2004/3/4 13:6:55</sys:DateTime>
</Button.Content>
</Button>
<Button Grid.Row="2" Margin="10,10,10,10">
<Button.Content>
<Rectangle Fill="Red" Width="100" Height="100">
</Rectangle>
</Button.Content>
</Button>
<Button Grid.Row="3" Margin="10,10,10,10">
<Button.Content>
<Ellipse Fill="Green" Width="100" Height="100">
</Ellipse>
</Button.Content>
</Button>
</Grid>
</Window>
包含一个标头和一个任意对象的控件
<Window x:Class="Example_13.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Example_13" mc:Ignorable="d" Title="Window1" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Expander Grid.Column="0" Name="myExpander" Background="Tan"
ExpandDirection="Down" IsExpanded="True">
<Expander.Header>
Expander Control
</Expander.Header>
<TextBlock TextWrapping="Wrap">
Expander 控件的Content属性
</TextBlock>
</Expander>
<GroupBox Grid.Column="1" Width="300" Height="410">
<GroupBox.Header>
<Label>Tab Control</Label>
</GroupBox.Header>
<StackPanel>
<TabControl Name="myTabControl" TabStripPlacement="Top" Margin="0, 0, 0, 10" Height="350">
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Ellipse Fill="Gray" Width="10" Height="10"></Ellipse>
<TextBlock Margin="5,0,0,0">Tab 1</TextBlock>
</StackPanel>
</TabItem.Header>
<StackPanel Orientation="Vertical">
<TextBlock>输入信息:</TextBlock>
<TextBox></TextBox>
</StackPanel>
</TabItem>
<TabItem>
<TabItem.Header>Tab 2</TabItem.Header>
<TextBlock>文本信息</TextBlock>
</TabItem>
</TabControl>
</StackPanel>
</GroupBox>
</Grid>
</Window>
ItemsControl类继承自Control,可以包含多个项,例如字符串、对象或其他元素。它的内容属性为ItemsSource和Items。ItemsSource 通常用于使用数据集合填充ItemsControl,如果不想使用集合填充 ItemsControl,可使用Items属性添加项。典型控件有(Menu、MenuBase、ContextMenu、ComboBox、ItemsControl、ListBox、ListView、TabControl、TreeView、Selector、StatusBar)我们可以通过下图可以看到Items属性返回ItemCollection和ItemsSource返回IEnumerable的对象
下图显示了包含这些类型的项的 ListBox:一个字符串、DateTime 对象、一个 UIElement、Panel包含一个 Ellipse 和一个 TextBlock
<Window x:Class="Example_13.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Example_13"
xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="Window2" Height="450" Width="800">
<Grid>
<ListBox>
<ListBoxItem>
字符创类型
</ListBoxItem>
<ListBoxItem>
<sys:DateTime>2004/3/4 13:6:55</sys:DateTime>
</ListBoxItem>
<ListBoxItem>
<Rectangle Fill="Red" Width="40" Height="40">
</Rectangle>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Ellipse Fill="Green" Width="30" Height="30"></Ellipse>
<TextBlock> TextBlock</TextBlock>
</StackPanel>
</ListBoxItem>
</ListBox>
</Grid>
</Window>
HeaderedItemsControl类继承自ItemsControl,可以包含多个项,例如字符串、对象或其他元素,也可以包含标题。它继承ItemsControl内容属性 ItemsSource 和 Items,并定义可以是任意对象的 Header 属性。典型的控件有(MenuItem、ToolBar、TreeViewItem)