# ItemTemplate

When binding your **RadTreeView** to a collection, you are able to configure the visualization of the data and the appearance of the **RadTreeViewItems** via the **ItemTemplate** and the **ItemTemplateSelector** properties.

If you want to read about the **ItemTemplateSelector**, see the main topic [ItemTemplateSelector](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/item-template-selector).

This tutorial uses Expression Blend to create a **HierarchicalDataTemplate** and assign it to the treeview's **ItemTemplate** property.

The resulting tree displays leagues with their divisions as child items:
![RadTreeView showing League A and League B, each expanded to display Division A, Division B, and Division C.](https://www.telerik.com/products/wpf/documentation/assets/4698e95f61195eeba571ceeb5eeffd1d/radtreeview_templatingitemtemplate_100.png)

* Create a new Silverlight application and perform the following steps:
  * Add a **RadTreeView** in your XAML.
  * Create two empty hierarchical data templates and one empty data template in your application resources as it is shown in the XAML below:
  ```XAML
  <UserControl.Resources>

      <DataTemplate x:Key="Team">
          <Grid>
          </Grid>
      </DataTemplate>

      <HierarchicalDataTemplate x:Key="Division">
          <Grid>
          </Grid>
      </HierarchicalDataTemplate>

      <HierarchicalDataTemplate x:Key="League">
          <Grid>
          </Grid>
      </HierarchicalDataTemplate>

  </UserControl.Resources>

  <Grid x:Name="LayoutRoot" Background="White">
      <telerik:RadTreeView x:Name="radTreeView" Margin="8"/>
  </Grid>
  ```

* Open your page in Expression Blend.

  In the **Resources** window (Window->Resources), in the context of your control you will see the three templates which were declared directly in the XAML on the previous step.
  ![Blend Resources pane listing the League, Division, and Team templates.](https://www.telerik.com/products/wpf/documentation/assets/686ab203752d6caa6027d11f60e0c518/radtreeview_templatingitemtemplate.png)

* Select **Edit resource** for the **League** template. Add an **Image** and a **TextBlock** to the template, as shown in the following XAML.
  ![League template containing a Grid with Image and TextBlock controls in the Objects and Timeline pane.](https://www.telerik.com/products/wpf/documentation/assets/154552ccb012e770a9c4ed12bf7962f0/radtreeview_templatingitemtemplate_010.png)

  ```XAML
  <Grid>
      <Grid.Resources>
          <HierarchicalDataTemplate x:Key="League" ItemsSource="{Binding Divisions, Mode=OneWay}"
  				ItemTemplate="{StaticResource Division}">
              <Grid d:DesignWidth="93" d:DesignHeight="26">
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="0.323*"/>
                      <ColumnDefinition Width="0.677*"/>
                  </Grid.ColumnDefinitions>
                  <Image Source="{Binding ImageUrl, Mode=OneWay}"/>
                  <TextBlock Margin="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name, Mode=OneWay}"/>
              </Grid>
          </HierarchicalDataTemplate>
  ```

  To configure the binding for the **Source** property of the **Image control**, select it in the "**Object and Timeline**" pane.

  ![Image selected within the League template in Objects and Timeline.](https://www.telerik.com/products/wpf/documentation/assets/c7d940218adc4c0e0affde9484ab95c6/radtreeview_templatingitemtemplate_020.png)

  Go to the "**Properties**" pane and find the **Source** property in the **Common properties** section.
  ![Image Source property in the Common Properties section.](https://www.telerik.com/products/wpf/documentation/assets/f1374d14570b23e6f3a9f1ea89dd0ee2/radtreeview_templatingitemtemplate_030.png)

  Open the **Source** property's options menu and choose **Data Binding** to open the **Create Data Binding** dialog.
  ![Source property options menu with the Data Binding command.](https://www.telerik.com/products/wpf/documentation/assets/b2f6ed64bf6fb985996477f36babd4a2/radtreeview_templatingitemtemplate_040.png)

  In the **Create Data Binding** dialog, select **Explicit Data Context** and check **Use a custom path expression**. Enter `ImageUrl` to bind the image's **Source** to that property. Select **OK**.
  ![Image Source binding dialog with ImageUrl entered as the custom path expression.](https://www.telerik.com/products/wpf/documentation/assets/ee877ffc5e15c4691c8c6a1b5b30cad6/radtreeview_templatingitemtemplate_050.png)

  Repeat the binding-dialog steps for **TextBlock.Text**, using the text-binding path in the completed **League** template below, not `ImageUrl`.

  Finally, in the "**Object and Timeline**" pane, select the **HierarchicalDataTemplate:**
  ![HierarchicalDataTemplate selected in Objects and Timeline.](https://www.telerik.com/products/wpf/documentation/assets/44efd9c86b67ad9803d55ec242f3e673/radtreeview_templatingitemtemplate_060.png)

  Find and set the following properties in the "**Properties**" pane:

  * **ItemsSource**: Set the binding expression to `{Binding Divisions, Mode=OneWay}`.

    ![ItemsSource property with a tooltip showing the one-way binding to Divisions.](https://www.telerik.com/products/wpf/documentation/assets/c73e0127e2a5bc8e724bd4f3b443839b/radtreeview_templatingitemtemplate_070.png)

  * **ItemTemplate**: Set the resource reference to `{StaticResource Division}`.

    ![ItemTemplate property referencing the Division resource.](https://www.telerik.com/products/wpf/documentation/assets/18dbb02c52e03dfbb1a0bbdfd35f6c7f/radtreeview_templatingitemtemplate_080.png)

  Here is the result XAML for the **League** data template:

  ```XAML
  <Grid>
      <Grid.Resources>
          <HierarchicalDataTemplate x:Key="League" ItemsSource="{Binding Divisions, Mode=OneWay}"
  				ItemTemplate="{StaticResource Division}">
              <Grid d:DesignWidth="93" d:DesignHeight="26">
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="0.323"/>
                      <ColumnDefinition Width="0.677"/>
                  </Grid.ColumnDefinitions>
                  <Image Source="{Binding ImageUrl, Mode=OneWay}"/>
                  <TextBlock Margin="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name, Mode=OneWay}"/>
              </Grid>
          </HierarchicalDataTemplate>
  ```

* Perform the same steps for the other two templates - **Division** and **Team**.

* Select the treeview and set its **ItemTemplate** property to the **League** resource (`{StaticResource League}`), as shown in the following XAML.
  ![Treeview ItemTemplate property options for selecting a template resource.](https://www.telerik.com/products/wpf/documentation/assets/0def62afc35b895c5be67a93789fdc98/radtreeview_templatingitemtemplate_090.png)

```XAML
<UserControl>
    <UserControl.Resources>
        <sampleData:RadTreeViewSampleData x:Key="DataSource"/>

        <DataTemplate x:Key="Team">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.323"/>
                    <ColumnDefinition Width="0.677"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding ImageUrl, Mode=OneWay}" Width="16" Height="16"/>
                <TextBlock Margin="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name, Mode=OneWay}"/>
            </Grid>
        </DataTemplate>

        <HierarchicalDataTemplate x:Key="Division"
	        ItemsSource="{Binding Teams, Mode=OneWay}"
	        ItemTemplate="{StaticResource Team}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.323"/>
                    <ColumnDefinition Width="0.677"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding ImageUrl, Mode=OneWay}" Width="16" Height="16"/>
                <TextBlock Margin="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name, Mode=OneWay}"/>
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="League" 
	        ItemsSource="{Binding Divisions, Mode=OneWay}"
	        ItemTemplate="{StaticResource Division}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.323"/>
                    <ColumnDefinition Width="0.677"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding ImageUrl, Mode=OneWay}" Width="16" Height="16"/>
                <TextBlock Margin="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name, Mode=OneWay}"/>
            </Grid>
        </HierarchicalDataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot2" Background="White">
        <telerik:RadTreeView x:Name="radTreeView2" Margin="8"
	       ItemsSource="{Binding Source={StaticResource DataSource}, Path=LeaguesDataSource}"
	       ItemTemplate="{StaticResource League}" />
    </Grid>
```

The data source class **RadTreeViewSampleData** assigned to the **RadTreeView** is covered in greater details in the chapter [Binding to Object](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/data-binding-to-object).

## See Also

* [ItemTemplateSelector](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/item-template-selector)
* [ItemEditTemplate](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/item-edit-template)
* [Hierarchical Data Templates](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/hierarchical-data-templates)
* [DataBinding - Overview](https://www.telerik.com/products/wpf/documentation/controls/radtreeview/populating-with-data/databinding-overview)
