tags:

views:

254

answers:

2

When databinding a TreeView, how would you create multiple levels of grouping for the items displayed?

Imagine you are binding to a Shop class which, among others, has the properties "Region" and "RetailChain". The desired effect is to have a TreeView that displays the shops in the following topology:

+ Region: California
|  + Walmart
|    + Walmart Pacific Beach
|    + Walmart Orange County
|    + Walmart San Diego
+ Region: New Jersey
   + Frys
   | + Frys Electronics NJ
   + Walmart
     + Walmart New Jersey

The clincher is this: Regions aren't related to Retailers, and vice versa, so the Shop (the leaf node) is the only common denominator.

Using CollectionViewSource only allows for a single level of grouping (despite the fact that the CollectionView class has properties that suggest multi-level grouping).

Using the HierarchicalDataTemplate only works for top-down topologies, which this scenario doesn't lend itself to.

Any suggestions on how to solve this?

A: 

You can always write code that will pre-process the data and create a set of classes for easier binding.

Nir
A: 

The answer to my problem turned out to be this:

Add multiple groupings (via PropertyGroupDescriptor) to the CollectionViewSource, then use a ListBox / DataGrid with multiple GroupStyles. The nett effect of this is that the leaf items (in my example, the stores) are grouped at two distinct levels.

Switching which PropertyGroupDescriptor appears first in the CollectionViewSource will change which property is the "root" level in the resulting tree.

Mark