Binding to Object
To bind the RadTreeView to a collection use its ItemsSource property and define HierarchicalDataTemplate that are needed to display the data from the collection. If you want the changes to the collection to be automatically reflected to the RadTreeView items, the collection should inherit from ObservableCollection, or to implement the INotifyPropertyChanged interface.
The following tutorial will guide you how to bind a RadTreeView to a collection of business objects.
The final result should look like the snapshot below:
-
First, you need to include the following assemblies in your XAML declaration:
- Telerik.Windows.Controls
- Telerik.Windows.Controls.Navigation
-
Create a new class named Team. The class has a single string property.
-
Create a new class named Division. The class has a single string property - Name and a collection with teams.
-
Create a new class named League. The class has a single string property and a collection with divisions objects. Practically, a collection of League objects will be the data source for the treeview.
-
Create a new class named RadTreeViewSampleData. This will be the data source (the model) for the RadTreeView. The class has a reference to an ObservableCollection of League objects and a single method which initializes the data.
-
The next step is to declare the RadTreeViewSampleData as a resource in your application.
The sampleData alias points to the assembly where your data source is located.
-
Since the data is hierarchical, you need to declare a HierarchicalDataTemplate. If you want to learn about the hierarchical data template, read the topic about Hierarchical Data Templates.
-
Finally, here is the treeview declaration. For ItemsSource is used the DataSource resource. For ItemTemplate is set the created in the previous step hierarchical data template.
If you run the demo, the final result should look like the snapshot below.