Hierarchical TreeView in WPF

Introduction

Bellow code example contains Hierarchical tree view

XAML Code

<TreeView HorizontalAlignment="Left" Height="307" VerticalAlignment="Top" Width="397" Name="filterableTreeview" SelectedItemChanged="filterableTreeview_SelectedItemChanged">

<TreeView.ItemTemplate>

<HierarchicalDataTemplate x:Name="lvl1" ItemsSource="{Binding Path=Juniors1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

<TextBlock FontWeight="Normal" FontSize="14" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<HierarchicalDataTemplate.ItemTemplate>

<HierarchicalDataTemplate ItemsSource="{Binding Path=Juniors1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

<TextBlock FontWeight="Normal" FontSize="14" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<HierarchicalDataTemplate.ItemTemplate >

<HierarchicalDataTemplate>

<TextBlock FontWeight="Normal" FontSize="14" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

</HierarchicalDataTemplate>

</HierarchicalDataTemplate.ItemTemplate>

</HierarchicalDataTemplate>

</HierarchicalDataTemplate.ItemTemplate>

</HierarchicalDataTemplate>

</TreeView.ItemTemplate>

</TreeView>

 
Code to bind items with treeview

C# Code
 

private void Window_Loaded_1(object sender, RoutedEventArgs e)

{

     List<Employee> emps = new List<Employee>();

    emps.Add(new Employee() { ID = 1, Name = "Kailash1" });

    emps.Add(new Employee() { ID = 2, Name = "Kailash2" });

    emps.Add(new Employee() { ID = 3, Name = "Kailash3" });

    emps.Add(new Employee() { ID = 4, Name = "Kailas4" });

    emps.Add(new Employee() { ID = 5, Name = "Kailas5" });

    Employee emp1 = new Employee();

    emp1.Name = "Juniors16";

    emp1.Juniors1 = new List<Employee>();

    emp1.Juniors1.Add(new Employee() { ID = 113, Name = "Juniors113" });

    emp1.Juniors1.Add(new Employee() { ID = 123, Name = "Juniors123" });

    emp1.Juniors1.Add(new Employee() { ID = 133, Name = "Juniors133" });

    emp1.Juniors1.Add(new Employee() { ID = 143, Name = "Juniors143" });

    emp1.Juniors1.Add(new Employee() { ID = 153, Name = "Juniors153" });

    Employee emp = new Employee();

    emp.Name = "Kailash6";

    emp.Juniors1 = new List<Employee>();

    emp.Juniors1.Add(new Employee() { ID = 11, Name = "Juniors11" });

    emp.Juniors1.Add(new Employee() { ID = 12, Name = "Juniors12" });

    emp.Juniors1.Add(new Employee() { ID = 13, Name = "Juniors13" });

    emp.Juniors1.Add(new Employee() { ID = 14, Name = "Juniors14" });

    emp.Juniors1.Add(new Employee() { ID = 15, Name = "Juniors15" });

    emp.Juniors1.Add(emp1);

    emps.Add(emp);

    this.filterableTreeview.ItemsSource = emps;

}

code to dynamically added child items to treeview item

C# Code
 

private void filterableTreeview_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

{

    //TreeViewItem TVI = (TreeViewItem)filterableTreeview.ItemContainerGenerator.ContainerFromItem(e.NewValue);

    //DirectoryInfo di = (DirectoryInfo)e.NewValue;

    //TVI.ItemsSource=di.GetDirectories();

    Employee emp = (Employee)e.NewValue;

    emp.Juniors1 = new List<Employee>();

    emp.Juniors1.Add(new Employee() { ID = 11, Name = "Juniors11" });

    emp.Juniors1.Add(new Employee() { ID = 12, Name = "Juniors12" });

    emp.Juniors1.Add(new Employee() { ID = 13, Name = "Juniors13" });

    emp.Juniors1.Add(new Employee() { ID = 14, Name = "Juniors14" });

    emp.Juniors1.Add(new Employee() { ID = 15, Name = "Juniors15" });

    e.Handled = true;

}

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now