ItemContainerStyleSelector
The Telerik RadTreeView supports ItemContainerStyle. The ItemContainerStyle property gives you the ability to change the treeview items' header. The container created by the RadTreeView for each item in the collection is of type RadTreeViewItem.
If you have different items and/or you prefer to display items with different styles, then you should use the ItemContainerStyleSelector property. This tutorial will walk you through the common task of creating and applying ItemContainerStyleSelector.
For the purpose of this tutorial will be used the following treeview declaration:
The data source class RadTreeViewSampleData assigned to the RadTreeView is covered in greater details in the chapter Binding to Object.
ItemContainerStyleSelector
The most common use of the "selectors" is to display different kind of data (different kind of items).
If you want to read more about HierarchicalDataTemplate and DataBinding, see the main topics about HierarchicalDataTemplate and Binding to Object.
-
Create three styles in the resources of your application.
-
LeagueItemContainerStyle
-
DivisionItemContainerStyle
-
TeamItemContainerStyle
The style defined for the ItemContainerStyle property should have as TargetType the RadTreeViewItem class.
These are the three Styles, which will be used as item container style. Accordingly, when the object type is League, then the LeagueItemContainerStyle will be applied; when the object type is Division, then the DivisionItemContainerStyle will be applied; when the object type is Team, then the TeamItemContainerStyle will be applied.
-
The next step is to create a selector where the logic about selecting the right style will be.
-
Create a new class, named LeagueItemContainerStyleSelector, which derives from StyleSelector.
If you create an ItemTemplateSelector or ItemEditTemplateSelector, you must derive from the DataTemplateSelector class. But if you want to create ItemContainerStyleSelector, you must derive from the StyleSelector class.
-
Override the SelectStyle method and implement your custom logic in it. The method accepts as arguments an object and a DependencyObject. The object argument is the actual object being bound and the DependecyObject is the container for it.
-
Define the created selector as a resource in your XAML and set it to the ItemContainerStyleSelector property.