Original Blog post can be found here
here[
^]
By default CollectionViewSource nests groupings when two or more property names are specified as GroupDescriptions. this is an attempt to group items in same level based on two or more property names.
In Short it can be expressed as,
1. Implement ICollectionView
2. Listen for GroupDescription changes,
3. When GroupDescription changed, get possible unique group names, and associated items.
4. Create CollectionViewGroup from the above combination.
5. Bind this as the item source.
void OnGroupDescriptionsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
groups.Clear();
var propNames = GroupDescriptions
.OfType<PropertyGroupDescription>()
.Select(pDesc => pDesc.PropertyName);
var groupNames = SourceCollection
.OfType<object>()
.Select(i => GetGroupName(i,propNames))
.Distinct();
var viewGroups = groupNames
.Select(gName => new CollectionViewGroup(gName,
SourceCollection
.OfType<object>()
.Where(i => GetGroupName(i, propNames).Equals(gName))));
foreach (object group in viewGroups)
groups.Add(group);
}
private string GetGroupName(object item, IEnumerable propNames)
{
var groupName = string.Empty;
foreach (string propertyName in propNames)
{
groupName += item.GetType()
.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public)
.GetValue(item, null)
.ToString() + " ";
}
return groupName;
}
Here is the
Sample[
^] Check it out and let me know your suggestions!!!
HaPpY Coding!!! :)