A
TreeView represents data in a hierarchical view in a parent child
relationship where a parent node can be expanded or collapse. The left
side bar of Windows Explorer is an example of a TreeView.
The TreeView tag represents a WPF TreeView control in XAML.
<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.
<TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="194" Height="200" />
Adding TreeView Items
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.
<TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="194" Height="200">
<TreeViewItem Header="Cold Drinks">
<TreeViewItem Header="Coke"></TreeViewItem>
<TreeViewItem Header="Pepsi"></TreeViewItem>
<TreeViewItem Header="Orange Juice"></TreeViewItem>
<TreeViewItem Header="Milk"></TreeViewItem>
<TreeViewItem Header="Iced Tea"></TreeViewItem>
<TreeViewItem Header="Mango Shake"></TreeViewItem>
</TreeViewItem>
</TreeView>
By default, the parent node is collapsed but when you click on it, the expanded view looks like Figure 1.

Figure 1. TreeView with items
Adding TreeView Items Dynamically
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 following:
<TextBox Height="23" HorizontalAlignment="Left" Margin="8,14,0,0"
Name="textBox1" VerticalAlignment="Top" Width="127" />
<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.

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.
private void button1_Click(object sender, RoutedEventArgs e)
{
TreeViewItem newChild = new TreeViewItem();
newChild.Header = textBox1.Text;
Parent.Items.Add(newChild);
}
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.
