Saadi
Saadi

Reputation: 2237

Group heading is not displayed after grouping in ItemsControl

I'm trying to group my ObservableCollection using CollectionViewSource, it seems to work for items but it's not showing the bind property value for GroupBox.

Here is what I'm trying:

I've List<Object> containing properties Description, Season. I want to group by Season. Here is my 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>

It's showing the Season value as GroupBox Heading. Is there anything wrong?

Upvotes: 0

Views: 824

Answers (1)

mm8
mm8

Reputation: 169280

You should bind to the Name property of the group:

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

This should display the actual value of the Season property that you group by.

Upvotes: 2

Related Questions