Binding HierarchicalDataTemplate with multiple child types

4 posts, 0 answers
  1. Nick Wood
    Nick Wood avatar
    126 posts
    Member since:
    Feb 2010

    Posted 27 Apr 2010 Link to this post

    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?




  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Apr 2010 Link to this post

    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.
  3. DevCraft banner
  4. Nick Wood
    Nick Wood avatar
    126 posts
    Member since:
    Feb 2010

    Posted 28 May 2010 Link to this post

    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


  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 03 Jun 2010 Link to this post

    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.
Back to Top