views:

275

answers:

2

I'm using a TreeView with a Hierarchical Data Template to bind to a View Model hierarchy, my problem is i have multiple child collections of different types (same base class though). Seems relatively simple to use the template to bind one of the collections but i'm struggling to work out how to do both.

class ParentViewModel 
{ 
  List<FooViewModel> FooCollection {get; set;}
  List<BarViewModel> BarCollection {get; set;}
}

This is kind of what i'm trying to achieve, but wondered if there's a simple way to do it:

http://www.codeplex.com/ComplexDataTemplates

A: 

There's no way that I know of. When I've needed to do this I've added another property to the ViewModel, called say, Children, which aggregates the two collections into one:

public IEnumerable<ViewModel> Children
{
    get
    {
        foreach (FooViewModel foo in FooCollection) yield return foo;
        foreach (BarViewModel bar in FooCollection) yield return bar;
    }
}
Groky
A: 

If your different collections are only on the root node, you can just add multiple treeviews, and bind each root to a different collection.

Alternativeley you can just add multiple treeviews within the HierarchicalDataTemplate and specify indiviual bindings for that each treeview.

I'm not sure if that will help your specific situation, but personally, I am using the treeview for Menu system, that binds to completeley different menu options.

Eli Perpinyal