Question

I have a wpf datagrid with grouped rows, implemented using a CollectionViewSource. It appears the group header templates bind directly to CollectionViewGroup objects, but for some reason these aren't very accessible inside the CVS. You give the CollectionViewSource the group names, and it handles generating the CVG's behind the scenes. This makes things difficult if you want the group header styles to bind to something other than what few properties the CVG's expose, like Name and ItemCount.

Basically, I want every group to have a Status property, probably to be visually indicated by the group header background color. This Status can change, so somehow the header will have to detect propertychanged notifications. But since CollectionViewGroup does not have a Status property, and I cannot supply my own CVGs to the CollectionViewSource, I've no idea how to do this simple task.

Was it helpful?

Solution

I figured it out eventually. The Name property of CollectionViewGroup is an object, so you can create a group view model of desired properties, then give that as the Name when adding group descriptions to the CollectionViewSource. And then in the xaml just do nested binding to Name.whatever for the group header controls.

I set it up like so (vb.net to follow):

  Me.BindedCV = New Data.CollectionViewSource
  Me.BindedCV.GroupDescriptions.Add(New Data.PropertyGroupDescription("ProductGroup"))

This means all rows (more specifically, the viewmodels that the rows are binded to) will be grouped according to a property called ProductGroup. Now I add my own group objects to the CollectionView group descriptions:

        Dim pg = New ProductGroupVM(pd.Index)
        Me._ProductGroupVMs.Add(pg)
        Me.BindedCV.GroupDescriptions(0).GroupNames.Add(pg)

So by adding pg to the GroupNames collection means it can now be referenced and binded to in the xaml group header styling - it is the Name object. Note that I also added pg to a second private collection I created called _ProductGroupVMs. This is a bit hackish, but that way I can keep a reference to all my group objects - when I create the row viewmodels, they will have a ProductGroup property, and ProductGroup needs to point to the right pg in order for them to be grouped correctly. There might be cleaner ways to do it but that's the route I went.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top