esse
esse

Reputation: 13

WPF TreeView binding to a simple object

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:

How can I achieve this through XAML?

Upvotes: 1

Views: 1313

Answers (1)

Gishu
Gishu

Reputation: 136663

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.

Upvotes: 1

Related Questions