This is a migrated thread and some comments may be shown as answers.

Binding HierarchicalDataTemplate with multiple child types

3 Answers 322 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nick Wood
Top achievements
Rank 1
Nick Wood asked on 27 Apr 2010, 07:10 AM
Hi All

There are many posts about the HierarchicalDataTemplate and how to bind to data that has a specific level structure, ie Categories contain products that contain orders etc.

This can be done in the following manner:
<core:HierarchicalDataTemplate x:Key="League" ItemsSource="{Binding Divisions}"
            <StackPanel Orientation="Horizontal"
                <Image Source="{Binding Image}" Margin=" 0,0,6,0" /> 
                <TextBlock Text="{Binding Name}" Foreground="Black" FontWeight="Bold" FontSize="15" /> 
            </StackPanel> 
        </core:HierarchicalDataTemplate> 

But what about if you have more complex structures such as categories with unlimited sub level categories and or categories that can contain both sub category and product types in the same level.

I cannot seem to make this type of layout work. Is this possible?




3 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 30 Apr 2010, 09:25 AM
Hi Nick Wood,

Please accept my apology for the delayed response.

Displaying Departments with unlimited sub levels of Departments for example can be done easily by using the following HierarchicalDataTemplate:
<telerik:HierarchicalDataTemplate x:Key="DepartmentTemplate"
                                          ItemsSource="{Binding Departments}"
                                          telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
            <TextBlock Text="{Binding Name}"/>
</telerik:HierarchicalDataTemplate>


As for the second scenario, it depends on how you define the business object you want to datatbind the RadTreeView to.

Now if you have a Category class and each category contains a collection of subcategories and products, you can put those two in a new class (SubItems for example, which contains a couple of properties - one of type Category and one of type Product) and you can create a collection of SubItems in the Category class. Then you can set the ItemsSource in the HierarchicalDataTemplate to SubItems and in the ItemTemplate you can specify what you need to display.

I prepared an example for you. Please take a look at it and let me know if this is what you had in mind. Also if your scenario is more complicated, you can send us a sample project and we will help you implement it.

I hope this will help you.

Kind regards,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Nick Wood
Top achievements
Rank 1
answered on 28 May 2010, 02:45 PM
Hi

Sorry, this is not quite what I am looking for. instead of having 2 object types in the same sub node, I need to have each type in a separate treeviewitem.

Here is a link to a WPF project that explains exactly what I am after:
http://complexdatatemplates.codeplex.com/

Thankyou very much for your help


0
Tina Stancheva
Telerik team
answered on 03 Jun 2010, 05:12 PM
Hello Nick Wood,

Please accept my apology for the delayed response and the misunderstanding.

I modified the example to illustrate the desired scenario.

Now the TreeView contains Categories which have as sub items both another categories and products. Both classes Category and Product derive from one common class SubItems.

Please take a look at the project and let me know if this is what you had in mind.

All the best,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Nick Wood
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Nick Wood
Top achievements
Rank 1
Share this question
or