wpf treeview binding

pojo picture pojo · Mar 14, 2011 · Viewed 49.7k times · Source

Is there any simple tutorial for beginners about treeview binding in WPF?

What should we write in ItemsSource, DataType, ItemTemplate attributes if there's one List of items?

IList<string> items = new List<string>();
items.Add("item1");
items.Add("item2");
items.Add("item3");

XAML code:

<TreeView Name="treeView1">  
    <TreeView.Resources> <!-- what does it mean? -->
        <HierarchicalDataTemplate DataType="???" ItemsSource="{Binding ???}"></HierarchicalDataTemplate>  
    </TreeView.Resources>  
</TreeView>

Answer

Sherlock picture Sherlock · Jun 18, 2013

To Fully understand how to use the wpf treeview with data binding, I went through the following tutorials in order -

1) A very simple example of treeview binding using recursion

http://testdrivendevelopment.wordpress.com/2008/07/15/databinding-wpf-treeview-using-recursion/

2) Claus Konrads simple example of data binding with the treeview. It's the most straightforward example I have come across and should get any newcomers to wpf up to speed.

http://blog.clauskonrad.net/2011/04/how-to-make-hierarchical-treeview.html

3) Mike Hillbergs tutorial shows, in detail, the ins and outs of the treeview, how it compares to other wpf controls, and how to bind data.

http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx