如何:对实现 GridView 的 ListView 中的项进行分组
更新:2007 年 11 月
此示例演示如何在 ListView 控件的 GridView 视图模式下显示项目组。
示例
若要在 ListView 中显示项目组,请定义 CollectionViewSource。下面的示例演示了一个按 Catalog 数据字段值对数据项进行分组的 CollectionViewSource。
<CollectionViewSource x:Key='src'
Source="{Binding Source={StaticResource MyData},
XPath=Item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Catalog" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
下面的示例将 ListView 的 ItemsSource 设置为前一个示例所定义的 CollectionViewSource。此示例还定义了实现 Expander 控件的 GroupStyle。
<ListView ItemsSource='{Binding Source={StaticResource src}}'
BorderThickness="0">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" BorderBrush="#FFA4B97F"
BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"
Margin="5,0,0,0" Width="100"/>
<TextBlock FontWeight="Bold"
Text="{Binding Path=ItemCount}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
...
</ListView>
有关完整示例,请参见 ListView 分组示例。