The grouping would work when you flatten this hierarchy i.e. "Contact having Groups" into "Repeated Contacts having each single group" ...
e.g.
If you have 4 items with groups like ...
Dog { mammal, quadruped }
Man { mammal, biped }
PrayingMantis { insect, quadruped }
Pegion { bird, biped }
Then you new flat list should be like this...
<mammal, Dog>
<mammal, Man>
<bird, Pigeon>
<insect, PrayingMantis>
<biped, Man>
<biped, Pigeon>
<quadruped, Dog>
<quadruped, PrayingMantis>
So after applyin grouping on the Keys
above it should be
mammal { Dog, Man }
bird { Pigeon }
insect { PrayingMantis }
biped { Man, Pigeon }
quadruped { Dog, PrayingMantis }
C# Code:
//Flatten the groups into a KeyValuePair<string, Contacts> list using LINQ.
var flatGroups
= listGroups.SelectMany(
ctc => ctc.Groups.Select(
grp => new KeyValuePair<string, Contact>(grp, ctc))).ToList();
//Apply CollectionViewSource group on the `Key`.
var collectionVwSrc = new CollectionViewSource();
collectionVwSrc.Source = flatGroups;
collectionVwSrc.GroupDescriptions.Add(new PropertyGroupDescription("Key"));
//Apply groups as itemssource to the TreeView.
MyGroupsTree.ItemsSource = collectionVwSrc.View.Groups;
XAML
<TreeView x:Name="MyGroupsTree">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<!--GroupItem.Name-->
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<!--Contact.Name-->
<TextBlock Text="{Binding Value.Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Let me know if this helps...