Dynamic Layered Nesting TreeView in WPF

I am trying to create an application in which I need to display employees and their departments in a tree structure, as shown below:

  • Employee1
    • The Department
      • dept1
      • Dept2
  • Employee2
    • The Department
      • Dept3
      • Dept4

How can I do this using WPF?

+5
source share
1 answer

The right way to do this is to use HierarchicalDataTemplate. The simplest I can imagine is the following:

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

What can be used in XAML as follows:

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

Of course, you can customize the template as you wish with styles and subcomponents.

, ItemSource TreeView TreeViewItem, TreeViewItem Items.

+1

All Articles