# In Code-Behind

This tutorial will walk you through the common tasks of **adding** and **removing RadTreeViewItems** programmatically.

Here is a snapshot of a regular treeview without treeview items:

![WPF RadTreeView Without Items](https://www.telerik.com/products/wpf/documentation/assets/182b3080856bc8b0ec51f1fab5995d42/radtreeview_populatingwithdatacodebehind___001.png)

```XAML
<telerik:RadTreeView x:Name="radTreeView" Background="Bisque" Margin="8"/>
```

## Adding TreeView Items

In order to add new treeview items to a treeview control, first you have to create an instance of the **RadTreeViewItem** class, set its properties like **Header**, **Foreground**, etc., and then add it to the treeview items collection.

```C#
private void AddTreeViewItems()
{
    RadTreeViewItem category = new RadTreeViewItem();
    category.Header = "Category1";
    category.Foreground = new SolidColorBrush(Colors.Green);
    radTreeView.Items.Add(category);
    category = new RadTreeViewItem();
    category.Header = "Category2";
    category.Foreground = new SolidColorBrush(Colors.Purple);
    radTreeView.Items.Add(category);
}
```

```VB.NET
	Private Sub AddTreeViewItems()
	    Dim category As New RadTreeViewItem()
	    category.Header = "Category1"
	    category.Foreground = New SolidColorBrush(Colors.Green)
	
	    radTreeView.Items.Add(category)
	
	    category = New RadTreeViewItem()
	    category.Header = "Category2"
	    category.Foreground = New SolidColorBrush(Colors.Purple)
	
	    radTreeView.Items.Add(category)
	End Sub
```

In order to create your hierarchical structure deeper, you just need to add **RadTreeViewItem** child nodes to any of the previously declared items. For example, here is the same **AddTreeViewItems()** method, but this time a little more extended:

```C#
private void AddTreeViewItems()
{
    RadTreeViewItem category = new RadTreeViewItem();
    category.Header = "Category1";
    category.Foreground = new SolidColorBrush(Colors.Green);
    radTreeView.Items.Add(category);

    // Adding child items
    RadTreeViewItem product = new RadTreeViewItem();
    product.Header = "Product1.1";
    category.Items.Add(product);
    product = new RadTreeViewItem();
    product.Header = "Product1.1";
    category.Items.Add(product);

    category = new RadTreeViewItem();
    category.Header = "Category2";
    category.Foreground = new SolidColorBrush(Colors.Purple);
    radTreeView.Items.Add(category);

    // Adding child items
    product = new RadTreeViewItem();
    product.Header = "Product2.1";
    category.Items.Add(product);
    product = new RadTreeViewItem();
    product.Header = "Product2.2";
    category.Items.Add(product);
}
```

```VB.NET
	Private Sub AddTreeViewItems()
	    Dim category As New RadTreeViewItem()
	    category.Header = "Category1"
	    category.Foreground = New SolidColorBrush(Colors.Green)
	
	    radTreeView.Items.Add(category)
	
	    ' Adding child items '
	    Dim product As New RadTreeViewItem()
	    product.Header = "Product1.1"
	
	    category.Items.Add(product)
	
	    product = New RadTreeViewItem()
	    product.Header = "Product1.1"
	
	    category.Items.Add(product)
	
	    category = New RadTreeViewItem()
	    category.Header = "Category2"
	    category.Foreground = New SolidColorBrush(Colors.Purple)
	
	    radTreeView.Items.Add(category)
	
	    ' Adding child items '
	    product = New RadTreeViewItem()
	    product.Header = "Product2.1"
	
	    category.Items.Add(product)
	
	    product = New RadTreeViewItem()
	    product.Header = "Product2.2"
	
	    category.Items.Add(product)
	End Sub
```

This will be the result if you execute the **AddTreeViewItems()** method:
![WPF RadTreeView Populated from Code-Behind](https://www.telerik.com/products/wpf/documentation/assets/94ecc81c8629c0760e90bd5030dc516d/radtreeview_populatingwithdatacodebehind___010.png)

> tip Consider declaring treeview items in XAML instead of adding them by code whenever it's possible. This includes situations when you know what items you need at design time.

## Removing TreeView Items

In order to remove a treeview item you have to remove it from the treeview items collection.

```C#
private void RemoveTreeViewItem(RadTreeViewItem itemToRemove)
{
    radTreeView.Items.Remove(itemToRemove);
}
```

```VB.NET
	Private Sub RemoveTreeViewItem(ByVal itemToRemove As RadTreeViewItem)
	    radTreeView.Items.Remove(itemToRemove)
	End Sub
```

## See Also

* [DataBinding - Overview](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/databinding-overview)
* [Populating with Data - Declaratively](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/declaratively)
* [Binding to XML](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/data-binding-to-xml)
* [Binding to Object](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/data-binding-to-object)
* [Binding to WCF Service](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/databinding-to-wcf-service)
* [Binding to ADO.NET Data Service](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/databinding-to-adonet-service)
