A是功能丰富

来源:互联网 发布:易语言辅助源码 编辑:程序博客网 时间:2024/06/06 13:58

ItemsControl: ‘A’ is for Abundance

这是我所谓的“ItemsControl:A-Z”的第一篇文章。

我最近收到了很多直接或者间接跟ItemsControl有关的问题。值得注意的是,提问者甚至没有意识到他们正在提问的是一个跟ItemsControl相关的问题。

所以对我来说,可能需要更好阐述一下这个类,包括它的用法,微妙之处,特点,还包括对其应用样式和模板以及将一个集合绑定其上。首先让我们从最基本的开始。

1.       什么是ItemsControl

非常简单,一个ItemsControl就是一个可以展示一个集合中的项的控件。

ItemsControl是一个功能丰富的控件,它是每个展示集合项控件的基类。包括但是不限于以下控件:

 

      

ComboBox

 

<ComboBox SelectedIndex="0">

  <ComboBoxItem>Item 1</ComboBoxItem>

  <ComboBoxItem>Item 2</ComboBoxItem>

  <ComboBoxItem>Item 3</ComboBoxItem>

</ComboBox>

 

 

      

ContextMenu
  Menu
  MenuItem

 

<ContextMenu>

  <MenuItem Header="New"/>

  <MenuItem Header="Open"/>

  <Separator/>

  <MenuItem Header="Submenu">

    <MenuItem Header="Submenu Item 1"/>

    <MenuItem Header="Submenu Item 2"/>

  </MenuItem>

</ContextMenu>

 

 

 

      

ListBox

 

<ListBox SelectedIndex="0" Width="100">

 <ListBoxItem>Item 1</ListBoxItem>

 <ListBoxItem>Item 2</ListBoxItem>

 <ListBoxItem>Item 3</ListBoxItem>

 <ListBoxItem>Item 4</ListBoxItem>

 <ListBoxItem>Item 5</ListBoxItem>

</ListBox>

 

 

      

ListView

 

<ListView SelectedIndex="0" Height="110"

   ItemsSource="{StaticResourceCharacters}">

 <ListView.View>

   <GridView>

     <GridViewColumn Width="100"

       DisplayMemberBinding="{Binding Last}"

       Header="Last Name" />

     <GridViewColumn Width="100"

       DisplayMemberBinding="{Binding First}"

       Header="First Name" />

     <GridViewColumn Width="60"

       DisplayMemberBinding="{Binding Gender}"

       Header="Gender" />

   </GridView>

 </ListView.View>

</ListView>

 

 

      

TabControl

 

<TabControl Width="150" Height="100">

 <TabItem Header="One">

   <TextBlock>Hello World</TextBlock>

 </TabItem>

 <TabItem Header="Two" />

 <TabItem Header="Three" />

</TabControl>

 

 

      

ToolBar

 

<ToolBar Margin="2">

  <Button Command="ApplicationCommands.Open"

     Height="40" Width="40"

     Background="{StaticResource OpenIcon}"/>

  <Button Command="ApplicationCommands.Save"

     Height="40" Width="40"

     Background="{StaticResource SaveIcon}"/>

</ToolBar>

 

 

      

TreeView
  TreeViewItem

 

<TreeView Width="150" Height="100"

   ItemsSource="{StaticResourceCharacters}">

 <TreeView.Resources>

   <HierarchicalDataTemplate

       DataType="{x:Type src:Character}"

       ItemsSource ="{Binding Fact}">

     <TextBlock Text="{Binding Name}" />

   </HierarchicalDataTemplate>

   <DataTemplate DataType="{x:Type src:Fact}">

     <StackPanel Orientation="Horizontal">

       <TextBlock Text="{Binding Name}" />

       <TextBlock Text=": " />

       <TextBlock Text="{Binding Value}" />

     </StackPanel>

   </DataTemplate>

 </TreeView.Resources>

</TreeView>

 

这个控件也有自己的权利

上面列出因为它们自己额外的功能而被经常使用。然而,需要注意的是,ItemsControl本身同样可以直接实例化,如下所示:

<Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition />
  </Grid.RowDefinitions>
  <ItemsControl VerticalAlignment="Top">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Button Command="{Binding}" Content="{Binding}"
          CommandTarget="{Binding ElementName=EditRegion}" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal" />
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <x:Static Member="ApplicationCommands.Cut" />
    <x:Static Member="ApplicationCommands.Copy" />
    <x:Static Member="ApplicationCommands.Paste" />
  </ItemsControl>
  <TextBox Name="EditRegion" Grid.Row="1" />
</Grid>

 

上面的标记创建了穷人的工具栏。在未来的章节里,我们将研究为什么我们需要直接使用ItemsControl,而不是比如使用ListBox等。

2.       Panel是ItemsControl吗?

不是,一个面板的逻辑孩子是UIElement,而ItemsControl的逻辑孩子是它的项,它可以是任何CLR对象。

备注:那么什么是面板?面板的主要角色是为孩子提供布局支持。虽然一个面板确实可以维护一个集合的子UI元素。技术上,它甚至不是一个WPF控件,因为它没有从Control继承也不支持模板。它其实只是一个具有单一目的元素,用来定位和排布它的孩子们。

3.       为什么我需要关心ItemsControl?

因为它是一个如何功能丰富的控件,理解应用到ItemsControl和其后代的的基本原则将会给我们带来好处。一旦你理解了ItemsControl,那么也能很容易理解其他例如ListBox之类的控件了。

假如有一天你偶尔发现有一个如下的声明:

<dw:Graph ItemsSource="{Binding Source={StaticResource OrgChartItems}}"
    ItemContainerStyle="{StaticResource NodeStyle}"
    ItemTemplate="{StaticResource EmployeeTemplate}"
    ItemsPanel="{StaticResource TreeGraphPanelTemplate}" />

 

装备了ItemsControl的相关知识,至少你就不会感到恐慌了。你会知道Graph对象其实是一个自定义的ItemsControl。你会立刻知道如何使用它的大部分功能。

4.       什么将会被谈及?

在将来的章节里,我希望谈及以下主题。我很确信,这不是一个很详尽的列表。但是你应该可以感到这个系列文章的方向了。

 

·          项集合: Items vs. ItemsSource

·          何时使用 ItemsControl

·          项模板

·          项容器

·          项容器产生器

·          项宿主(or ItemsPanel)

·          ItemsControl样式化和模板化

·          遍历容器和模板树

·          HeaderedItemsControl

·          Headered ItemsControl样式化和模板化

·          创建自定义 ItemsControl 和项容器

·          UI 虚拟化

 

 

原创粉丝点击