方法 : GridView を実装する ListView の項目をグループ化する
この例では、 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>
次の例では、前の例で定義される CollectionViewSource に、ListView の ItemsSource が設定されます。 また、この例では 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>