Philipp Rosengart
Philipp Rosengart

Reputation: 733

C# WPF Listbox Group Header Style

I'm using a Listbox to show multiple items. The items are grouped. Now I want to edit the header style but the only thing happens is that the text isn't shown anymore.

Thats my xaml:

<ListBox x:Name="lbTreatments" HorizontalAlignment="Left" Height="473" Margin="10,37,0,0" VerticalAlignment="Top" Width="309" FontSize="13">
    <ListBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding subcategoryName}" FontWeight="Bold"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And thats my code to group the items:

ICollectionView view = CollectionViewSource.GetDefaultView(treatmentsCategory);
view.GroupDescriptions.Add(new PropertyGroupDescription("subcategoryName"));
view.SortDescriptions.Add(new SortDescription("subcategoryName", ListSortDirection.Ascending));
lbTreatments.ItemsSource = view;

The items are grouped but the header text is missing. If I delete the Groupstyle from xaml the text will be shown. Can anybode help me please?

Upvotes: 3

Views: 2626

Answers (1)

Fruchtzwerg
Fruchtzwerg

Reputation: 11399

MSDN says:

Each group is of type CollectionViewGroup.

The actual data type is CollectionViewGroup which doesn't have a subcategoryName property so the binding will fail. Instead you have to use the Name property, which will already be set to the value from subcategoryName.

As such, use this...

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

...in your header template to get the group name.

Upvotes: 1

Related Questions