Using XAML TreeView in WPF

The XAML TreeView element represents a TreeView control. This tutorial shows you how to use a TreeView in WPF.

Introduction

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.

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 that is a unique identifier of a control. The Margin property specifies 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 TreeView Items

A TreeView control hosts a collection of TreeViewItems. 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"   
  2.  VerticalAlignment="Top" Width="194" Height="200">  
  3.     <TreeViewItem Header="Cold Drinks">  
  4.         <TreeViewItem Header="Coke"></TreeViewItem>  
  5.         <TreeViewItem Header="Pepsi"></TreeViewItem>  
  6.         <TreeViewItem Header="Orange Juice"></TreeViewItem>  
  7.         <TreeViewItem Header="Milk"></TreeViewItem>  
  8.         <TreeViewItem Header="Iced Tea"></TreeViewItem>  
  9.         <TreeViewItem Header="Mango Shake"></TreeViewItem>  
  10.     </TreeViewItem>  
  11. </TreeView>  
By default, the parent node is collapsed but when you click on it, the expanded view looks as in the following.

TreeView with items
                     Figure 1. TreeView with items

In the 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 looks as in following:
  1. <TextBox Height="23" HorizontalAlignment="Left" Margin="8,14,0,0"   
  2.                  Name="textBox1" VerticalAlignment="Top" Width="127" />  
  3. <Button Height="23" Margin="140,14,0,0" Name="button1" VerticalAlignment="Top"   
  4.                 HorizontalAlignment="Left" Width="76" Click="button1_Click">  
  5.             Add Item  
  6. </Button>  
The final UI looks as in the following. On the Add Item button, the click event handler is implemented and we will add a new item to the first parent node of the TreeView.

add item
                                                                  Figure 2.

On the button click event handler, we add the content of a TextBox to the TreeViewItem using the TreeViewItem.Items.Add method. The following code adds TextBox contents to the TreeViewItem items.
  1. private void button1_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     TreeViewItem newChild = new TreeViewItem();  
  4.     newChild.Header = textBox1.Text;  
  5.     Parent.Items.Add(newChild);  
  6. }  
In the button click event handler, we add the content of a TextBox to the TreeView using the TreeViewItem.Items.Add method.

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

Adding TreeView items dynamically
                                 Figure 3. Adding TreeView items dynamically

We can use TreeView.Items.Remove or the 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 as in the following:
  1. <Button Height="23" Margin="226,14,124,0" Name="DeleteButton"   
  2. VerticalAlignment="Top" Click="DeleteButton_Click">  
  3. Delete Item</Button>  
The button click event handler looks as in the following. On this button click, we find the index of the selected item and use the TreeView.Items.RemoveAt method as in the following.
  1. private void DeleteButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    TreeView1.Items.RemoveAt  
  4.        (TreeView1.Items.IndexOf(TreeView1.SelectedItem));                    
  5. }  
The preceding code removes root items from the TreeView, not the sub-items. To remove sub items, we need to find the selected item and then we need to use the TreeViewItem.Items.RemoveAt method.

A TreeView control is placed inside a StackPanel that contains a ScrollViewer control so when the width or height of the panel is more than the visible area, the scroll viewer becomes 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"/>              
  4.         <Setter Property="FontSize" Value="12"/>  
  5.         <Setter Property="FontWeight" Value="Bold" />  
  6.     </Style>  
  7. </Window.Resources>  
The new TreeView looks as in the following:

Formatted TreeView
                                             Figure 4. Formatted TreeView

We can put any controls inside a TreeViewItem such as an image or text. To display an image side by side 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 image and text to a TreeViewItem. The key here is to add an 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 as in the following:

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

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 change the code of TreeViewItems and add the following CheckBoxes to the items. As you may see, I have set the name of the CheckBoxes using the 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>  
The new TreeView looks as in the following:

TreeView with CheckBoxes
                                    Figure 6. TreeView with CheckBoxes

Before I discuss data binding in general, I must confess, the Microsoft experts have made a big mess related to data-binding in .NET 3.0 and 3.5. Instead of making things simpler, they have made them complicated. Maybe they have bigger plans in the future, but so far I have seen binding using dependency objects and properties, LINQ, DLINQ, WCF and ASP.NET Web Services and it all looks like a big mess. It's not even close to the ADO.NET model we had in .NET 1.0 and 2.0. I hope they solve this problem in the near future.

When it comes to data binding, we need to first understand the data. Here is a list of the ways data can be consumed: 
  • Objects
  • A relational database such as SQL Server
  • A XML file or
  • Other controls

The ItemsSource property of a TreeView is used to bind a collection of IEnuemerables such as an ArrayList to the TreeView control.

  1. // Bind ArrayList with the TreeView  
  2. LeftTreeView.ItemsSource = LoadTreeViewData();              
  3.   
  4. private ArrayList LoadTreeViewData()  
  5. {  
  6.     ArrayList itemsList = new ArrayList();  
  7.     itemsList.Add("Coffie");  
  8.     itemsList.Add("Tea");  
  9.     itemsList.Add("Orange Juice");  
  10.     itemsList.Add("Milk");  
  11.     itemsList.Add("Mango Shake");  
  12.     itemsList.Add("Iced Tea");  
  13.     itemsList.Add("Soda");  
  14.     itemsList.Add("Water");  
  15.     return itemsList;  
  16. }  
We've seen many requirements where a page has two TreeView controls and the left TreeView displays a list of items. Using a button, we can use items from the left TreeView and add them to the right side TreeView. Using the remove button we can remove items from the right side TreeView and add them back to the left side TreeView.

This sample shows how to move items from one TreeView to another. The final page looks as in the following. The Add button adds the selected item to the right side TreeView and removes it from the left side TreeView. The Remove button removes the selected item from the right side TreeView and adds back to the left side TreeView.

TreeView
                                                                                    Figure 7

right side TreeView
                                                                                 Figure 8

The following XAML code generates two TreeView controls and two Button controls.
  1. <TreeView Margin="11,13,355,11" Name="LeftTreeView" />  
  2. <TreeView Margin="0,13,21,11" Name="RightTreeView" HorizontalAlignment="Right" Width="216" />  
  3. <Button Name="AddButton" Height="23" Margin="248,78,261,0" VerticalAlignment="Top"  
  4.         Click="AddButton_Click">Add >></Button>  
  5. <Button Name="RemoveButton" Margin="248,121,261,117"   
  6.         Click="RemoveButton_Click"><< Remove</Button>  
On the Window loaded event, we create and load data items to the TreeView by setting the ItemsSource property to an ArrayList.
  1. private void Window_Loaded(object sender, RoutedEventArgs e)  
  2. {  
  3.     // Get data from somewhere and fill in my local ArrayList  
  4.     myDataList = LoadTreeViewData();  
  5.     // Bind ArrayList with the TreeView  
  6.     LeftTreeView.ItemsSource = myDataList;              
  7. }  
  8.   
  9. /// <summary>  
  10. /// Generate data. This method can bring data from a database or XML file  
  11. /// or from a Web service or generate data dynamically  
  12. /// </summary>  
  13. /// <returns></returns>  
  14. private ArrayList LoadTreeViewData()  
  15. {  
  16.     ArrayList itemsList = new ArrayList();  
  17.     itemsList.Add("Coffie");  
  18.     itemsList.Add("Tea");  
  19.     itemsList.Add("Orange Juice");  
  20.     itemsList.Add("Milk");  
  21.     itemsList.Add("Mango Shake");  
  22.     itemsList.Add("Iced Tea");  
  23.     itemsList.Add("Soda");  
  24.     itemsList.Add("Water");  
  25.     return itemsList;  
  26. }  
In the Add button click event handler, we get the value and index of the selected item in the left side TreeView and add that to the right side TreeView and remove that item from the ArrayList that is our data source. The ApplyBinding method simply removes the current binding of the TreeView and rebinds with the updated ArrayList.
  1. private void AddButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     // Find the right item and it's value and index  
  4.     currentItemText = LeftTreeView.SelectedValue.ToString();  
  5.     currentItemIndex = LeftTreeView.SelectedIndex;  
  6.       
  7.     RightTreeView.Items.Add(currentItemText);  
  8.     if (myDataList != null)  
  9.     {  
  10.         myDataList.RemoveAt(currentItemIndex);  
  11.     }  
  12.   
  13.     // Refresh data binding  
  14.     ApplyDataBinding();  
  15. }  
  16.   
  17.   
  18. /// <summary>  
  19. /// Refreshes data binding  
  20. /// </summary>  
  21. private void ApplyDataBinding()  
  22. {  
  23.     LeftTreeView.ItemsSource = null;  
  24.     // Bind ArrayList with the TreeView  
  25.     LeftTreeView.ItemsSource = myDataList;  
  26. }  
Similarly, on the Remove button click event handler, we get the selected item text and index from the right side TreeView and add that to the ArrayList and remove from the right side TreeView.
  1. private void RemoveButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     // Find the right item and it's value and index  
  4.     currentItemText = RightTreeView.SelectedValue.ToString();  
  5.     currentItemIndex = RightTreeView.SelectedIndex;  
  6.     // Add RightTreeView item to the ArrayList  
  7.     myDataList.Add(currentItemText);  
  8.   
  9.   RightTreeView.Items.RemoveAt(RightTreeView.Items.IndexOf(RightTreeView.SelectedItem));  
  10.   
  11.     // Refresh data binding  
  12.     ApplyDataBinding();  
  13. }  
We use the Northwind.mdf database that comes with SQL Server. In our application, we will read data from the Customers table. The Customers table columns look like this.

Customers table columns
                                                Figure 9

We will read ContactName, Address, City and Country columns in a WPF TreeView control. The final TreeView looks as in the following:

final TreeView
                                                                           Figure 10

Now let's look at our XAML file. We create a resources DataTemplate type called TreeViewTemplate. A data template is used to represent data in a formatted way. The data template has two dock panels where the first panel shows the name and the second panel shows the address, city and country columns by using TextBlock controls.
  1. <Window.Resources>  
  2.     <DataTemplate x:Key="TreeViewTemplate">  
  3.         <StackPanel Margin="3">  
  4.             <DockPanel >  
  5.                 <TextBlock FontWeight="Bold" Text="Name:"  
  6.                   DockPanel.Dock="Left"  
  7.                   Margin="5,0,10,0"/>  
  8.                 <TextBlock Text="  " />  
  9.                 <TextBlock Text="{Binding ContactName}" Foreground="Green" FontWeight="Bold" />  
  10.             </DockPanel>  
  11.             <DockPanel >  
  12.                 <TextBlock FontWeight="Bold" Text="Address:" Foreground ="DarkOrange"   
  13.                   DockPanel.Dock="Left"  
  14.                   Margin="5,0,5,0"/>  
  15.                 <TextBlock Text="{Binding Address}" />  
  16.                  <TextBlock Text=", " />  
  17.                 <TextBlock Text="{Binding City}" />  
  18.                  <TextBlock Text=", " />  
  19.                 <TextBlock Text="{Binding Country}" />  
  20.             </DockPanel>  
  21.         </StackPanel>  
  22.     </DataTemplate>  
  23. </Window.Resources>    
Now we add a TreeView control and set its ItemsSource property as the first DataTable of the DataSet and set ItemTemplate to the resource defined above.
  1. <TreeView Margin="17,8,15,26" Name="TreeView1" ItemsSource="{Binding Tables[0]}"   
  2. ItemTemplate="{StaticResource TreeViewTemplate}" />  
Now in our code behind, we define the following variables.
  1. public SqlConnection connection;   
  2. public SqlCommand command;   
  3. string sql = "SELECT ContactName, Address, City, Country FROM Customers";  
  4. string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";  
On the Windows_Loaded method, we use the BindData method. In the BindData method, we create a connection and a data adapter and fill in the DataSet using the SqlDataAdapter.Fill method.
  1. private void Window_Loaded(object sender, RoutedEventArgs e)  
  2. {  
  3.     BindData();             
  4. }  
  5.   
  6. private void BindData()  
  7. {  
  8.     DataSet dtSet = new DataSet();  
  9.     using (connection = new SqlConnection(connectionString))  
  10.     {  
  11.         command = new SqlCommand(sql, connection);                 
  12.         SqlDataAdapter adapter = new SqlDataAdapter();             
  13.         connection.Open();  
  14.         adapter.SelectCommand = command;  
  15.         adapter.Fill(dtSet, "Customers");  
  16.         TreeView1.DataContext = dtSet;  
  17.         
  18.     }  
  19. }  
Now let's look at how to bind XML data to a TreeView control. The XmlDataProvider is used to bind XML data in WPF.

Here is an XmlDataProvider defined in XAML that contains books data. The XML data is defined within the x:Data tag.
  1. <XmlDataProvider x:Key="BooksData" XPath="Inventory/Books">  
  2.     <x:XData>  
  3.         <Inventory xmlns="">  
  4.             <Books>  
  5.                 <Book Category="Programming" >  
  6.                     <Title>A Programmer's Guide to ADO.NET</Title>  
  7.                     <Summary>Learn how to write database applications using ADO.NET and C#.  
  8.                     </Summary>  
  9.                     <Author>Mahesh Chand</Author>  
  10.                     <Publisher>APress</Publisher>  
  11.                 </Book>  
  12.                 <Book Category="Programming" >  
  13.                     <Title>Graphics Programming with GDI+</Title>  
  14.                     <Summary>Learn how to write graphics applications using GDI+ and C#.  
  15.                     </Summary>  
  16.                     <Author>Mahesh Chand</Author>  
  17.                     <Publisher>Addison Wesley</Publisher>  
  18.                 </Book>  
  19.                 <Book Category="Programming" >  
  20.                     <Title>Visual C#</Title>  
  21.                     <Summary>Learn how to write C# applications.  
  22.                     </Summary>  
  23.                     <Author>Mike Gold</Author>  
  24.                     <Publisher>APress</Publisher>  
  25.                 </Book>  
  26.                 <Book Category="Programming" >  
  27.                     <Title>Introducing Microsoft .NET</Title>  
  28.                     <Summary>Programming .NET  
  29.                     </Summary>  
  30.                     <Author>Mathew Cochran</Author>  
  31.                     <Publisher>APress</Publisher>  
  32.                 </Book>  
  33.                 <Book Category="Database" >  
  34.                     <Title>DBA Express</Title>  
  35.                     <Summary>DBA's Handbook  
  36.                     </Summary>  
  37.                     <Author>Mahesh Chand</Author>  
  38.                     <Publisher>Microsoft</Publisher>  
  39.                 </Book>  
  40.             </Books>  
  41.              
  42.         </Inventory>  
  43.     </x:XData>  
  44. </XmlDataProvider>  
To bind an XmlDataProvider, we set the Source property inside the ItemsSource of a TreeView to the x:Key of XmlDataProvider and the code in the code is used to filter the data. In the TreeView.ItemTempate, we use the Binding property.
  1. <TreeView Width="400" Height="300" Background="LightGray">  
  2.     <TreeView.ItemsSource>  
  3.         <Binding Source="{StaticResource BooksData}"  
  4.        XPath="*[@Category='Programming'] "/>  
  5.     </TreeView.ItemsSource>  
  6.   
  7.     <TreeView.ItemTemplate>  
  8.         <DataTemplate>  
  9.             <StackPanel Orientation="Horizontal">  
  10.                 <TextBlock Text="Title: " FontWeight="Bold"/>  
  11.                 <TextBlock Foreground="Green"  >  
  12.                     <TextBlock.Text>   
  13.                         <Binding XPath="Title"/>  
  14.                     </TextBlock.Text>                        
  15.                 </TextBlock>                       
  16.            </StackPanel>  
  17.         </DataTemplate>  
  18.     </TreeView.ItemTemplate>  
  19. </TreeView>  
The output of the preceding code looks as in the following.

xml data binding
                                                               Figure 11

The last data binding type we will see is how to provide data exchange between a TreeView and other controls using data binding in WPF.

We will create an application that looks as in the following. I have a TreeView with a list of colors, a TextBox and a Canvas. When we pick a color from the TreeView, the text of the TextBox and color of the Canvas changes dynamically to the color selected in the TreeView. This is possible to do all in XAML without writing a single line of code in the code behind file.

code in the code
                                                           Figure 12.

The XAML code of the page looks as in the following:
  1. <StackPanel Orientation="Vertical">  
  2.     <TextBlock Margin="10,10,10,10" FontWeight="Bold">  
  3.         Pick a color from below list  
  4.     </TextBlock>  
  5.     <TreeView Name="mcTreeView" Height="100" Width="100"  
  6.              Margin="10,10,0,0" HorizontalAlignment="Left" >  
  7.         <TreeViewItem>Orange</TreeViewItem>  
  8.         <TreeViewItem>Green</TreeViewItem>  
  9.         <TreeViewItem>Blue</TreeViewItem>  
  10.         <TreeViewItem>Gray</TreeViewItem>  
  11.         <TreeViewItem>LightGray</TreeViewItem>  
  12.         <TreeViewItem>Red</TreeViewItem>  
  13.     </TreeView>   
  14.    <TextBox Height="23" Name="textBox1" Width="120" Margin="10,10,0,0" HorizontalAlignment="Left"  >  
  15.         <TextBox.Text>  
  16.             <Binding ElementName="mcTreeView" Path="SelectedItem.Content"/>  
  17.         </TextBox.Text>  
  18.     </TextBox>  
  19.     <Canvas Margin="10,10,0,0" Height="200" Width="200" HorizontalAlignment="Left" >  
  20.         <Canvas.Background>  
  21.             <Binding ElementName="mcTreeView" Path="SelectedItem.Content"/>  
  22.         </Canvas.Background>  
  23.     </Canvas>  
  24.   
  25. </StackPanel>     
If you look at the TextBox XAML code, you will see the Binding within the TextBox.Text property that sets the binding from the TextBox to another control. Another control ID is ElementName and another control's property is Path. So in the following code, we are setting the SelectedItem.Content property of the TreeView to the TextBox.Text property.
  1. <TextBox.Text>  
  2.     <Binding ElementName="mcTreeView" Path="SelectedItem.Content"/>  
  3. </TextBox.Text>  
The same applies to the Canvas.Background property, where we set it to the SelectedItem.Content of the TreeView. Now, every time you select an item in the TreeView, the TextBox.Text and Canvas.Background properties are set to the selected item in the TreeView.
  1. <Canvas.Background>  
  2.     <Binding ElementName="mcTreeView" Path="SelectedItem.Content"/>  
  3. </Canvas.Background>  
Summary

In this article, I exlained how to create and use a TreeView control available in WPF. We saw how to add items to a TreeView, change item properties, add images and add check boxes. In the end of this article, we saw how data binding works for a TreeView and how to bind a TreeView with data coming from objects, a database and other controls.

I hope you enjoyed this article. Please feel free to submit any questions or comments.

Up Next
    Ebook Download
    View all
    Learn
    View all