在ItemsControl中分组后,不显示组标题

时间:2017-01-19 08:09:26

标签: c# wpf mvvm data-binding observablecollection

我尝试使用ObservableCollectionCollectionViewSource进行分组,它似乎适用于项目,但它没有显示GroupBox的绑定属性值。

以下是我的尝试:

List<Object>包含属性说明,季节。我想按季节分组。这是我的xml:

<mah:MetroWindow x:Class="eCatalog_Launcher.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:eCatalog_Launcher"
             xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
             mc:Ignorable="d"
             Title="eCatalog Launcher"
             WindowState="Maximized"
             Loaded="MetroWindow_Loaded"
             Closing="MetroWindow_Closing">

<Window.Resources>
    <CollectionViewSource x:Key="catalogsBySeasons"
                          Source="{Binding Path=Catalogs, Mode=TwoWay}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Season" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>

<ScrollViewer>
    <ItemsControl ItemsSource="{Binding Source={StaticResource catalogsBySeasons}}">
        <ItemsControl.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <GroupBox Header="{Binding Season}">
                                        <ItemsPresenter />
                                    </GroupBox>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ItemsControl.GroupStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <mah:Tile Title="{Binding Description}"
                          Tag="{Binding}"
                          Style="{StaticResource SmallTileStyle}"
                          Click="Tile_Click">
                    <iconPacks:PackIconMaterial Width="32"
                                                Height="32"
                                                Margin="0, -30, 0, 0"
                                                Kind="{Binding Kind}">
                    </iconPacks:PackIconMaterial>
                </mah:Tile>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</StackPanel>

它将季节值显示为GroupBox标题。 有什么不对的吗?

1 个答案:

答案 0 :(得分:1)

您应该绑定到群组的名称属性:

<ControlTemplate TargetType="{x:Type GroupItem}">
    <GroupBox Header="{Binding Name}">
        <ItemsPresenter />
    </GroupBox>
</ControlTemplate>

这应该显示您分组的Season属性的实际值。

相关问题