I’m using a ListCollectionView as an ItemsSource for a WPF DataGrid.
I want the user to be able to add columns to group by, and I’m using the following as the GroupStyle:
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Group Name: "/>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Path=ItemCount}" FontStyle="Italic"/>
<TextBlock Text=" Items" FontStyle="Italic"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter>
</ItemsPresenter>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
My problem is that when there is more then 1 column in the GroupDescriptions, then group headings are displayed without indentation. Another problem is that I would like the TextBox that has “Group Name:” to bind to the Column name that is grouping that level – So if I’m groupint at that level on column = Gender it would say “Gender: “.
So how can I indent the group heading according to its nesting level in the GroupDescriptions collection, and how can I bind to the Column name?
No one stepped up to this, so with a lot of tinkering, I came up with the following solution. I created a multi value converter, taking the current CollectionViewGroup, the whole ListCollectionView and the DataGrid as parameters.
The CollectionViewGroup is used to find its parent with the GetParent function. This is the non-elegant part of the solution as it relies on capturing an error. The function is called until the error indicates reaching the top group.
ListCollectionView is used to get the sort column, and the DataGrid to get the more friendly Column Header.
HTH someone looking at a similar problem.