views:

346

answers:

5

When I make a list box in WPF I frequently set its ItemsSource to be a List. Is there a Tree for TreeView (or what goes in ItemsSource for TreeView)?

Is there a collection or generally accepted method for handling tree data in C#.NET?

A: 

It is certainly possible to data bind a source to a WPF TreeView instance. Here are a couple of blogs and sample entries on the subject

JaredPar
+2  A: 

Nothing built in as far as I know. What I usually do is something like this:

class User  
{  
string Name { get; set; }  
List<User> { get; set; }  
}  

Then you can use that to bind to your hierarchical control, such as a TreeView.

Henrik Söderlund
Actually, in WPF or Silverlight you probably want to use ObservableCollection instead of List. Then the UI will update automatically when you change your collection
Henrik Söderlund
A: 

There is an open source project that was originally by Wintellect, found here, called PowerCollections. Maybe there is something there that might help you?

Hope this helps, Best regards, Tom.

tommieb75
A: 

You might want to look at binding XML via XLinq to your treeview, such as here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/02a47e46-12e9-45fb-af18-4511f2212acb/ and here: http://www.beacosta.com/blog/?p=50.

ebpower
+4  A: 

What you want to do is bind a collection of hierarchical objects to the tree view using the Hierarchical Data Template.

I have written a blog post on this very subject, check it out,

Displaying Hierarchical Data with the WPF Tree View control

mattdlong
This was exactly what I needed. Did you ever write the one about lazy loading?
Vaccano
Yep, planning to get to the lazy loading early next week, check back then. Glad I could help.
mattdlong
Good Stuff! Thanks for updating it.
Vaccano