26

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>
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
pojo
  • 307
  • 1
  • 4
  • 4
  • 2
    You should take a step back and try to figure out what each of those things are before trying to bind a TreeView. Just follow a beginners WPF tuturial - there are heaps out there. – Greg Sansom Mar 14 '11 at 07:03

4 Answers4

33

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/

  1. 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

  1. 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

Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38
Sherlock
  • 5,557
  • 6
  • 50
  • 78
  • 1
    Claus Kondrad's example is great. – Ondrej Janacek May 01 '14 at 18:01
  • The first example needed a couple of tweaks to get working - I got rid of "Path=" in the bindings and added a Window.DataContext entry for local:Service where xmlns:local="clr-namespace:SimpleWpfTreeView" for a project called "SimpleWpfTreeView" – GTAE86 Apr 23 '21 at 20:52
  • 1
    Claus Konrad's blog appears to be unavailable so too is Mike Hillberg's tutorial on MSDN which just defaults to Microsoft Dev Blog front page – Moon Waxing Oct 06 '21 at 07:53
13

The trick is that ItemsSource points to the next collection down.

e.g Imagine you have a collection of type A, and each A contains a description and a collection of type B; and each B contains a description and a collection of type C. The binding would look like this:

<TreeView Width="400" ItemsSource="{Binding CollectionOfA}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type TypeA}" ItemsSource="{Binding CollectionOfB}">
            <TreeViewItem Header="{Binding TypeADescription}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type TypeB}" ItemsSource="{Binding CollectionOfC}">
            <TreeViewItem Header="{Binding TypeBDescription" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type TypeC}">
            <TreeViewItem Header="{Binding TypeC}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
geometrikal
  • 3,195
  • 2
  • 29
  • 40
  • Seeing as TypeC items have no children, you can just use a simple DataTemplate, rather than a HierarchicalDataTemplate for those items. – Peregrine Nov 06 '18 at 12:52
6

Have a look at Josh Smiths excellent tutorial

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
0

Treeview is one control in wpf that you have to appoach in a little diffrent manner.It is simple and efficient and at the same time a pain to understand and get in track for a beginer,especially those coming from the windows appliaction backgroud.Please go through the MVVM pattern first and then try to approach the treeview.

The Josh Smith article below is a good place to start.

http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

biju
  • 17,554
  • 10
  • 59
  • 95