TreeView In WPF

Introducing WPF TreeView

A TreeView represents data in a hierarchical view in a parent child relationship where a parent node can be expanded or collapsed. The left side bar of Windows Explorer is an example of a TreeView.

In this tutorial, we will create a WPF application that will add and delete a TreeView items dynamically. The UI looks like the following where anything entered in the TextBox will be added to the TreeView as a child node. 

The TreeView tag represents a WPF TreeView control in XAML.

  1. <TreeView></TreeView>  

The Width and Height properties represent the width and the height of a TreeView. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a TreeView on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.

The following code snippet sets the name, height, and width of a TreeView control. The code also sets horizontal alignment to left and vertical alignment to top.

  1. <TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left"  
  2. VerticalAlignment="Top" Width="194" Height="200" />  

Adding Items to a WPF TreeView

A TreeView control hosts a collection of TreeViewItem. The Header property is the text of the item that is displayed on the view. The following code snippet adds a parent item and six child items to a TreeView control.

  1. <TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200">  
  2.     <TreeViewItem Header="Cold Drinks">  
  3.         <TreeViewItem Header="Coke"></TreeViewItem>  
  4.         <TreeViewItem Header="Pepsi"></TreeViewItem>  
  5.         <TreeViewItem Header="Orange Juice"></TreeViewItem>  
  6.         <TreeViewItem Header="Milk"></TreeViewItem>  
  7.         <TreeViewItem Header="Iced Tea"></TreeViewItem>  
  8.         <TreeViewItem Header="Mango Shake"></TreeViewItem>  
  9.     </TreeViewItem>  
  10. </TreeView>  

By default, the parent node is collapsed but when you click on it, the expanded view looks like Figure 1.

 TreeView with items
Figure 1. TreeView with items 

Adding WPF TreeView Items Dynamically using C#

In previous section, we saw how to add items to a TreeView at design-time from XAML. We can add items to a TreeView from the code.

Let's change our UI and add a TextBox and a button control to the page. The XAML code for the TextBox and Button controls look like the following:

  1. <TextBox Height="23" HorizontalAlignment="Left" Margin="8,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="127" />  
  2. <Button Height="23" Margin="140,14,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76" Click="button1_Click"> Add Item </Button>  

The final UI looks like Figure 2. On Add Item button click event handler, we are going to add a new item to the first parent node of the TreeView.

Adding TreeView Items Dynamically
Figure 2.

On button click event handler, we add the content of TextBox to the TreeViewItem by calling TreeViewItem.Items.Add method. The following code adds TextBox contents to the TreeViewItem items.

  1. private void button1_Click(object sender, RoutedEventArgs e) {    
  2.     TreeViewItem newChild = new TreeViewItem();    
  3.     newChild.Header = textBox1.Text;    
  4.     Parent.Items.Add(newChild);    
  5. }   
On button click event handler, we add the content of TextBox to the TreeView by calling TreeViewItem.Items.Add method.

Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the TreeView.

Adding TreeView items dynamically
Figure 3. Adding TreeView items dynamically 

Deleting WPF TreeView Items

We can use TreeView.Items.Remove or TreeView.Items.RemoveAt method to delete an item from the collection of items in the TreeView. The RemoveAt method takes the index of the item in the collection.

Now, we modify our application and add a new button called Delete Item. The XAML code for this button looks like below.

  1. <Button Height="23" Margin="226,14,124,0" Name="DeleteButton" VerticalAlignment="Top" Click="DeleteButton_Click"> Delete Item</Button>  

The button click event handler looks like following. On this button click, we find the index of the selected item and call TreeView.Items.RemoveAt method as following.

  1. private void DeleteButton_Click(object sender, RoutedEventArgs e) {    
  2.     TreeView1.Items.RemoveAt(TreeView1.Items.IndexOf(TreeView1.SelectedItem));    
  3. }   
The above code removes root items from the TreeView, not the subitems. To remove sub items, first we need to find the selected item and then we need to call TreeViewItem.Items.RemoveAt method.


Styling a WPF TreeView Items


A TreeView control is placed inside a StackPanel that contains a ScrollVewer control so when the width of height of the panel is more than the visible area, the scroll viewer gets active and provides horizontal and vertical scrolling functionality.

To style a TreeView, we can use individual TreeViewItems and set their properties. Alternatively, we can use System.Resources and set Style property. The following code snippet sets TreeViewItem foreground, font size, and font weight properties.

  1. <Window.Resources>  
  2.     <Style TargetType="{x:Type TreeViewItem}">  
  3.         <Setter Property="Foreground"Value="Blue"/><Setter Property="FontSize"Value="12"/><Setter Property="FontWeight"Value="Bold"/>  
  4.     </Style>  
  5. </Window.Resources>  

The new TreeView looks like Figure 4.

Formatted TreeView 
Figure 4. Formatted TreeView


Loading Images in a WPF TreeView

We can put any controls inside a TreeViewItem such as an image and text. To display an image beside some text, I simply put an Image and TextBlock control within a StackPanel. The Image.Source property takes the name of the image you would like to display in the Image control and TextBlock.Text property takes a string that you would like to display in the TextBlock.

The following code snippet adds an image and text to a TreeViewItem. The key here is to add image and text to the header of TreeViewItems.

  1. <TreeViewItem Name="Child1">  
  2.     <TreeViewItem.Header>  
  3.         <StackPanel Orientation="Horizontal">  
  4.             <Image Source="coffie.jpg" Height="30"></Image>  
  5.             <TextBlock Text="Coffie"></TextBlock>  
  6.         </StackPanel>  
  7.     </TreeViewItem.Header>  
  8. </TreeViewItem>  

After changing my code for all 5 TreeViewItems, the TreeView looks like Figure 5.

TreeViewItems with Image and text
Figure 5. TreeViewItems with Image and text 

WPF TreeView with CheckBoxes

If you put a CheckBox control inside TreeViewItems, you generate a TreeView control with checkboxes in it. The CheckBox can host controls within it as well. For instance, we can put an image and text block as content of a CheckBox.

The following code snippet adds a CheckBox with an image and text to a TreeViewItem.

  1. <TreeViewItem Name="Child1">  
  2.     <TreeViewItem.Header>  
  3.         <CheckBox Name="CoffieCheckBox">  
  4.             <StackPanel Orientation="Horizontal">  
  5.                 <Image Source="coffie.jpg" Height="30"></Image>  
  6.                 <TextBlock Text="Coffie"></TextBlock>  
  7.             </StackPanel>  
  8.         </CheckBox>  
  9.     </TreeViewItem.Header>  
  10. </TreeViewItem>  

I changed the code of TreeViewItems and added the following CheckBoxes to the items. As you may see, I have set the name of the CheckBoxes using Name property. If you need to access these CheckBoxes, you may access them in the code using their Name property.

  1. <TreeViewItem Name="Child1">  
  2.     <TreeViewItem.Header>  
  3.         <CheckBox Name="CoffieCheckBox">  
  4.             <StackPanel Orientation="Horizontal">  
  5.                 <Image Source="coffie.jpg" Height="30"></Image>  
  6.                 <TextBlock Text="Coffie"></TextBlock>  
  7.             </StackPanel>  
  8.         </CheckBox>  
  9.     </TreeViewItem.Header>  
  10. </TreeViewItem>  
  11. <TreeViewItem Name="Child2">  
  12.     <TreeViewItem.Header>  
  13.         <CheckBox Name="IcedTeaCheckBox">  
  14.             <StackPanel Orientation="Horizontal">  
  15.                 <Image Source="IcedTea.jpg" Height="30"></Image>  
  16.                 <TextBlock Text="Iced Tea"></TextBlock>  
  17.             </StackPanel>  
  18.         </CheckBox>  
  19.     </TreeViewItem.Header>  
  20. </TreeViewItem>  
  21. <TreeViewItem Name="Child3">  
  22.     <TreeViewItem.Header>  
  23.         <CheckBox Name="MangoShakeCheckBox">  
  24.             <StackPanel Orientation="Horizontal">  
  25.                 <Image Source="MangoShake.jpg" Height="30"></Image>  
  26.                 <TextBlock Text="Mango Shake"></TextBlock>  
  27.             </StackPanel>  
  28.         </CheckBox>  
  29.     </TreeViewItem.Header>  
  30. </TreeViewItem>  
  31. <TreeViewItem Name="Child4">  
  32.     <TreeViewItem.Header>  
  33.         <CheckBox Name="MilkCheckBox">  
  34.             <StackPanel Orientation="Horizontal">  
  35.                 <Image Source="Milk.jpg" Height="30"></Image>  
  36.                 <TextBlock Text="Milk"></TextBlock>  
  37.             </StackPanel>  
  38.         </CheckBox>  
  39.     </TreeViewItem.Header>  
  40. </TreeViewItem>  
  41. <TreeViewItem Name="Child5">  
  42.     <TreeViewItem.Header>  
  43.         <CheckBox Name="TeaCheckBox">  
  44.             <StackPanel Orientation="Horizontal">  
  45.                 <Image Source="Tea.jpg" Height="30"></Image>  
  46.                 <TextBlock Text="Tea"></TextBlock>  
  47.             </StackPanel>  
  48.         </CheckBox>  
  49.     </TreeViewItem.Header>  
  50. </TreeViewItem>  
  51. <TreeViewItem Name="Child6">  
  52.     <TreeViewItem.Header>  
  53.         <CheckBox Name="OrangeJuiceCheckBox">  
  54.             <StackPanel Orientation="Horizontal">  
  55.                 <Image Source="OrangeJuice.jpg" Height="30"></Image>  
  56.                 <TextBlock Text="Orange Juice"></TextBlock>  
  57.             </StackPanel>  
  58.         </CheckBox>  
  59.     </TreeViewItem.Header>  
  60. </TreeViewItem>  
  61. </TreeViewItem>  

Now, the new TreeView looks like Figure 6.

TreeView with CheckBoxes 
Figure 6. TreeView with CheckBoxes 


This tutorial showed how to use a WPF TreeView control using XAML and C#. You also learned how to add check boxes and images to TreeView items.

Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.