views:

106

answers:

1

I have a simple object as such:

public class Info
{
    public string Name {get; set;}
    public int Count {get; set;}
    public DateTime TimeStamp {get; set;}
}

I want to bind a collection of these objects to a WPF TreeView and have the properties on the Info objects show up as sub TreeViewItems, like so:

  • Item 1
    • Name: Bill
    • Count: 3
    • TimeStamp: 12/05/2010 09:06:00 AM
  • Item 2
    • Name: Chris
    • Count: 22
    • TimeStamp: 11/05/2010 11:34:00 AM
  • Item 3
    • Name: Toby
    • Count: 1
    • TimeStamp: 09/05/2010 05:55:00 PM

How can I achieve this through XAML?

A: 

Create a InfoViewModel which has

  (Prop)  Item Index : 1
  (Prop)  Children   : ["Bill", 3, timestampvalue]

Map your list of Info objects to a list of InfoViewModels.

DataBind your tree to this list ; use DataContext and ItemsSource to point to your List

Define a HierarchicalDataTemplate for InfoViewModel and bind the ItemsSource property to InfoViewModel.Children. See this question for similar code sample.

Gishu
Thanks - works perfectly!
esse