We already saw an example of applying multiple group style here. Now we are going to do the same thing with ListView. The basic concept is almost similar. The only different is how we are going to bind and display items in these controls. At first step we are going to define two data template resources in XAML, one for each group header. Here is a XAML code for it.
Then we are going to add one class in our project. That class is inherited by DataTemplateSelector and define two DataTemplate type properties in it. We also override SelectTemplate method.
But now we have a problem. Here container is in fact CollectionViewGroupInternal class, which is inherited by CollectionViewGroup class. And we can’t access that class directly from our code, at least not easily.
But we know that our grouping is only two level depth therefore we can apply little trick here. If it is a first level grouping then all of items in this group is also a type of CollectionViewGroupInternal type. On the other hand if it is second level of grouping then its items type is StateInfo class. We are going to use this information and update our overridden method. Here is our new overridden method.
Now it works in this case. But of course there is limitation, we can’t use the same technique if the grouping is more than two level depth. Here is a complete XAML code of our program.
Here is complete C# code of our program.
Here is the output of our program.