Edit this page

Bind RadTreeView ImageSource Properties

RadTreeView API gives you the ability to define images for each item state (default, expanded and selected). For more information, take a look at the Item Images topic. This topic will walk you through the common task of binding the RadTreeViewItem's DefaultImageSrc property to a property of a business object. The following cases will be demonstrated:

Using Style Binding

Suppose that your business object has a property of type string, which stores the path to the image. This property will be bound to the DefaultImageSrc property of the RadTreeViewItem. The easiest way to bind the image path property of the business object to the RadTreeViewItem's DefaultImageSrc property is to define the binding in a style. See the code snippet below:


<Style x:Key="ItemContainerStyle" TargetType="telerik:RadTreeViewItem">
    <Setter Property="DefaultImageSrc" Value="{Binding Path=ImagePath}"/>
<telerik:RadTreeView x:Name="radTreeView" ItemContainerStyle="{StaticResource ItemContainerStyle}"/>

Using Style Selectors

Another possible scenario is to display different images for the different types of business objects. This is a perfect case for using a style selector. The solution here is to define a style for each type of business object. Each style should set the DefaultImageSrc/SelectedImageSrc/ExpandedImageSrc properties. Second, you should create a style selector where to implement the logic for selecting the right style. And finally, set the style selector to the ItemContainerStyleSelector. property of the RadTreeView. For more information take a look at the ItemContainerStyle and ItemContainerStyleSelector. topics.

The disadvantage of this approach is that all image paths are static (defined in XAML) and it is not possible to change them. So whenever your application logic allows this, bind the Default/Selected/ExpandedImageSrc property to a property of the business object.