应用Items属性
使用Items属性
随 WPF 附带的每个 ItemsControl 具有一个对应的类,该类代表 ItemsControl 中的一个项。下表列出了随 WPF 附带的 ItemsControl 对象及其相应的项容器。
ItemsControl 项容器
ComboBox ComboBoxItem
ContextMenu MenuItem
ListBox ListBoxItem
ListView ListViewItem
Menu MenuItem
StatusBar StatusBarItem
TabControl TabItem
TreeView TreeViewItem
<Window x:Class="WpfApplication1.使用Items属性"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="使用Items属性" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="82*" />
<RowDefinition Height="96*" />
<RowDefinition Height="84*" />
</Grid.RowDefinitions>
<!--方法一-->
<ListBox Grid.Row="0">
<ListBox.Items>
<ListBoxItem>
Item A
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="Item B" />
</ListBoxItem>
</ListBox.Items>
</ListBox>
<!--方法二,省略ListBox.Items-->
<ListBox Grid.Row="1">
<ListBoxItem>
Item A
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="Item B" />
</ListBoxItem>
</ListBox>
<!--方法三,省略ListBoxItem-->
<ListBox Grid.Row="2">
<ListBox.Items>
Item A
<TextBlock Text="Item B" />
</ListBox.Items>
</ListBox>
</Grid>
</Window>
四、HeaderedItemsControl模型
HeaderedItemsControl 从 ItemsControl 类继承。HeaderedItemsControl 定义 Header 属性,该属性遵从相同的规则,因为 HeaderedContentControl. WPF 的 Header 属性附带三个从 HeaderedItemsControl 继承的控件:MenuItem、ToolBar、TreeViewItem
HeaderedItemsControl模型可以理解为如下结构:一个HeaderedItemsControl包含一个Items集合,每一个Item包含一个Header属性,一个子Items集合,以TreeView和TreeViewItem为例:
<Window x:Class="WpfApplication1.HeaderedItemsControl模型"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HeaderedItemsControl模型" Height="300" Width="300">
<Canvas>
<TreeView>
<TreeView.Items>
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="Root Node A" />
</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock Text="A - 1" />
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="A - 2" />
</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock Text="A - 2 - 1" />
<TextBlock Text="A - 2 - 2" />
<TextBlock Text="A - 2 - 3" />
</TreeViewItem.Items>
</TreeViewItem>
</TreeViewItem.Items>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>Tree Node B</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock>B-1</TextBlock>
<TreeViewItem>
<TreeViewItem.Header>
B_B_1
</TreeViewItem.Header>
<TextBlock>B-B_1</TextBlock>
<TextBlock>B-B_2</TextBlock>
</TreeViewItem>
</TreeViewItem.Items>
</TreeViewItem>
</TreeView.Items>
</TreeView>
</Canvas>
</Window>
随 WPF 附带的每个 ItemsControl 具有一个对应的类,该类代表 ItemsControl 中的一个项。下表列出了随 WPF 附带的 ItemsControl 对象及其相应的项容器。
ItemsControl 项容器
ComboBox ComboBoxItem
ContextMenu MenuItem
ListBox ListBoxItem
ListView ListViewItem
Menu MenuItem
StatusBar StatusBarItem
TabControl TabItem
TreeView TreeViewItem
<Window x:Class="WpfApplication1.使用Items属性"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="使用Items属性" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="82*" />
<RowDefinition Height="96*" />
<RowDefinition Height="84*" />
</Grid.RowDefinitions>
<!--方法一-->
<ListBox Grid.Row="0">
<ListBox.Items>
<ListBoxItem>
Item A
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="Item B" />
</ListBoxItem>
</ListBox.Items>
</ListBox>
<!--方法二,省略ListBox.Items-->
<ListBox Grid.Row="1">
<ListBoxItem>
Item A
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="Item B" />
</ListBoxItem>
</ListBox>
<!--方法三,省略ListBoxItem-->
<ListBox Grid.Row="2">
<ListBox.Items>
Item A
<TextBlock Text="Item B" />
</ListBox.Items>
</ListBox>
</Grid>
</Window>
四、HeaderedItemsControl模型
HeaderedItemsControl 从 ItemsControl 类继承。HeaderedItemsControl 定义 Header 属性,该属性遵从相同的规则,因为 HeaderedContentControl. WPF 的 Header 属性附带三个从 HeaderedItemsControl 继承的控件:MenuItem、ToolBar、TreeViewItem
HeaderedItemsControl模型可以理解为如下结构:一个HeaderedItemsControl包含一个Items集合,每一个Item包含一个Header属性,一个子Items集合,以TreeView和TreeViewItem为例:
<Window x:Class="WpfApplication1.HeaderedItemsControl模型"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HeaderedItemsControl模型" Height="300" Width="300">
<Canvas>
<TreeView>
<TreeView.Items>
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="Root Node A" />
</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock Text="A - 1" />
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="A - 2" />
</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock Text="A - 2 - 1" />
<TextBlock Text="A - 2 - 2" />
<TextBlock Text="A - 2 - 3" />
</TreeViewItem.Items>
</TreeViewItem>
</TreeViewItem.Items>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>Tree Node B</TreeViewItem.Header>
<TreeViewItem.Items>
<TextBlock>B-1</TextBlock>
<TreeViewItem>
<TreeViewItem.Header>
B_B_1
</TreeViewItem.Header>
<TextBlock>B-B_1</TextBlock>
<TextBlock>B-B_2</TextBlock>
</TreeViewItem>
</TreeViewItem.Items>
</TreeViewItem>
</TreeView.Items>
</TreeView>
</Canvas>
</Window>