New to Telerik UI for WPF? Download free 30-day trial

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: WPF RadTreeView Binding to ObservableCollection

  • 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.

        public class Team 
        { 
            public Team( string name ) 
            { 
                this.Name = name; 
            } 
            public string Name 
            { 
                get; 
                set; 
            } 
        } 
  • Create a new class named Division. The class has a single string property - Name and a collection with teams.

        public class Division 
        { 
            public Division( string name ) 
            { 
                this.Name = name; 
                this.Teams = new ObservableCollection<Team>(); 
            } 
            public string Name 
            { 
                get; 
                set; 
            } 
            public ObservableCollection<Team> Teams 
            { 
                get; 
                set; 
            } 
        } 
  • 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.

        public class League 
        { 
            public League( string name ) 
            { 
                this.Name = name; 
                this.Divisions = new ObservableCollection<Division>(); 
            } 
            public string Name 
            { 
                get; 
                set; 
            } 
            public ObservableCollection<Division> Divisions 
            { 
                get; 
                set; 
            } 
        } 
  • 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.

        public class RadTreeViewSampleData 
        { 
            public RadTreeViewSampleData() 
            { 
                this.InitializeLeaguesDataSource(); 
            } 
            public ObservableCollection<League> LeaguesDataSource 
            { 
                get; 
                set; 
            } 
            private void InitializeLeaguesDataSource() 
            { 
                this.LeaguesDataSource = new ObservableCollection<League>(); 
                League l; 
                Division d; 
                this.LeaguesDataSource.Add( l = new League( "League A" ) ); 
                l.Divisions.Add( ( d = new Division( "Division A" ) ) ); 
                d.Teams.Add( new Team( "Team I" ) ); 
                d.Teams.Add( new Team( "Team II" ) ); 
                d.Teams.Add( new Team( "Team III" ) ); 
                d.Teams.Add( new Team( "Team IV" ) ); 
                d.Teams.Add( new Team( "Team V" ) ); 
                l.Divisions.Add( ( d = new Division( "Division B" ) ) ); 
                d.Teams.Add( new Team( "Team Blue" ) ); 
                d.Teams.Add( new Team( "Team Red" ) ); 
                d.Teams.Add( new Team( "Team Yellow" ) ); 
                d.Teams.Add( new Team( "Team Green" ) ); 
                d.Teams.Add( new Team( "Team Orange" ) ); 
                l.Divisions.Add( ( d = new Division( "Division C" ) ) ); 
                d.Teams.Add( new Team( "Team East" ) ); 
                d.Teams.Add( new Team( "Team West" ) ); 
                d.Teams.Add( new Team( "Team North" ) ); 
                d.Teams.Add( new Team( "Team South" ) ); 
                this.LeaguesDataSource.Add( l = new League( "League B" ) ); 
                l.Divisions.Add( ( d = new Division( "Division A" ) ) ); 
                d.Teams.Add( new Team( "Team 1" ) ); 
                d.Teams.Add( new Team( "Team 2" ) ); 
                d.Teams.Add( new Team( "Team 3" ) ); 
                d.Teams.Add( new Team( "Team 4" ) ); 
                d.Teams.Add( new Team( "Team 5" ) ); 
                l.Divisions.Add( ( d = new Division( "Division B" ) ) ); 
                d.Teams.Add( new Team( "Team Diamond" ) ); 
                d.Teams.Add( new Team( "Team Heart" ) ); 
                d.Teams.Add( new Team( "Team Club" ) ); 
                d.Teams.Add( new Team( "Team Spade" ) ); 
                l.Divisions.Add( ( d = new Division( "Division C" ) ) ); 
                d.Teams.Add( new Team( "Team Alpha" ) ); 
                d.Teams.Add( new Team( "Team Beta" ) ); 
                d.Teams.Add( new Team( "Team Gamma" ) ); 
                d.Teams.Add( new Team( "Team Delta" ) ); 
                d.Teams.Add( new Team( "Team Epsilon" ) ); 
            } 
        } 
  • The next step is to declare the RadTreeViewSampleData as a resource in your application.

        <Window.Resources> 
            <sampleData:RadTreeViewSampleData x:Key="DataSource"/> 
        </Window.Resources> 

    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.

        <Window.Resources> 
            <sampleData:RadTreeViewSampleData x:Key="DataSource"/> 
     
            <DataTemplate x:Key="Team"> 
                <TextBlock Text="{Binding Name}" /> 
            </DataTemplate>     
            <HierarchicalDataTemplate x:Key="Division" ItemTemplate="{StaticResource Team}" 
                   ItemsSource="{Binding Teams}"> 
                <TextBlock Text="{Binding Name}" /> 
            </HierarchicalDataTemplate> 
            <HierarchicalDataTemplate x:Key="League" ItemTemplate="{StaticResource Division}" 
                   ItemsSource="{Binding Divisions}"> 
                <TextBlock Text="{Binding Name}" /> 
            </HierarchicalDataTemplate> 
     
        </Window.Resources> 
  • 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.

        <telerik:RadTreeView 
               ItemsSource="{Binding Source={StaticResource DataSource}, Path=LeaguesDataSource}" 
               ItemTemplate="{StaticResource League}" /> 
  • If you run the demo, the final result should look like the snapshot below. WPF RadTreeView Binding to ObservableCollection