Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi level Nested TreeView with Dynamic Binding in WPF

I am trying to create an application in which i require to display employees and their departments in the treeview kind of structure as below :

  • Employee1
    • Department
      • Dept1
      • Dept2
  • Employee2
    • Department
      • Dept3
      • Dept4

how could i do this with WPF ?

like image 753
saloni Avatar asked Nov 04 '22 20:11

saloni


1 Answers

The correct way to do this is to use a HierarchicalDataTemplate. The most basic one I can imagine is the following:

<UserControl.Resources>
        <HierarchicalDataTemplate
            x:Key="RecursiveData" DataType="TreeViewItem" ItemsSource="{Binding Items}">
        </HierarchicalDataTemplate>
    </UserControl.Resources>

Which can be used in the XAML as follows:

<TreeView ItemTemplate="{StaticResource RecursiveData}" />

Of course you can customize the template at will with styles and subcomponents.

Note that the ItemSource of your TreeView needs to actually provide nested TreeViewItems where each TreeViewItem contains it's subitems in Items.

like image 107
Vogel612 Avatar answered Nov 15 '22 09:11

Vogel612