Working with ItemPrepared Event
The purpose of this tutorial is to show you how to work with the RadTreeView's ItemPrepared event. This event occurs when a child RadTreeViewItem has been prepared and is now ready for use. The main goal of this event is to perform binding of RadTreeViewItem properties to the data objects.
The example shown in this tutorial will demonstrate you how to bind the check state of individual treeview items to a ToggleState property of a class.
In few words, the approaches that you should follow are:
-
Create a sample data source
For the purpose of this example the following business objects will be used:
The CheckState property of the DataItem class will be bound to the CheckState property of the RadTreeViewItem.
The RawDataSource does nothing special, except for initializing the sample data for the RadTreeView. This is the data source for the treeview.
-
Add a new RadTreeView declaration. Enable the following properties:
- IsOptionElementsEnabled
- IsTriStateMode
-
Declare a RawDataSource object as a resource in your XAML and set the RadTreeView's ItemsSource property.
-
Declare a new HierarchicalDataTemplate in your XAML resources and set the RadTreeView's ItemTemplate property.
-
The final and most important step is to attach to the ItemPrepared event of the RadTreeView.
Switch to the code-behind and in the event handler add a code, synchronizing the CheckState property of the RadTreeViewItem object with the CheckState property of the DataItem object.
-
The example so far is almost complete. However, there are some drawbacks. The RadTreeViewItem's CheckState property is synchronized just once. If the user checks\unchecks a specific treeview item, the source object will not be updated. In order to solve this problem you should attach to the RadTreeViewItem's Checked and Unchecked events.