Felix D. Felix D. - 23 days ago 9
C# Question

Issue with binding in DataGrid GoupStyle

I am displaying jobs. Jobs got states like this:

public enum JobState
{
Done,
Running,
Overdue,
Disabled
}


When displaying the jobs in my
DataGrid
I group them by
State
, which is working as expected.

I followed this example. The problem is that I would like to add a header with the
Name
of the
State
to each grouping section.

Im just not sure what exactly is the
DataContext
of the
GroupItem
.

Maybe a
RelativeSource
helps ? - I was not able to make it ... any recommendations ?




<DataGrid ItemsSource="{Binding Path=JobCollectionView}" [..]>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=State}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
//Error is here: <TextBlock Text="{Binding Path=StateDescription}" />
<TextBlock Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" Jobs"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>

Evk Evk
Answer

The DataContext of GroupItem is internal class named CollectionViewGroupInternal. You already know that number of items is represented by ItemCount on that class. The rest of the puzzle - name of group is represented by Name property :) Since that is just enum in your case - you can bind directly to that Name property (it contains an instance of your JobState enum).