Recursive problem with HierarchicalDataTemplate and DataTemplateSelector

4 posts, 0 answers
  1. Nurullah
    Nurullah avatar
    6 posts
    Member since:
    Dec 2009

    Posted 05 Apr 2013 Link to this post

    Hello,

    I have a problem in my treeview with the recursion in HierarchicalDataTemplates.

    First of all, what I'm trying to achieve is a tree like this;

    Type-A1
    Header1
    Type-B1
    Type-B2
    Header2
    Type-A2
    Header1
    Type-B3
    Type-B4
    Header2
    Type-A4
    Type-A3
    Type-A5
    Header1
    Type-B5
    Header2
    Type-A6
    Type-A7

    and my Type-A object has a Type-A list and a Type-B list. But in order to use it with a HierarchicalDataTemplate i'm combining these two lists in a single list of two lists like this;

    List<object> CombinedList = new List<object>(){TypeAList, TypeBList};

    And I'm differentiating the lists by their type in a DataTemplateSelector.

    For this, I tried using the following;
    <UserControl.Resources>
        <telerik:HierarchicalDataTemplate x:Key="TemplateB">
            <TextBlock Text="{Binding something}"/>
        </telerik:HierarchicalDataTemplate>
     
        <telerik:HierarchicalDataTemplate x:Key="HeaderTemplateB" ItemsSource="{Binding}" ItemTemplate="{StaticResource TemplateB}">
            <TextBlock Text="Header1" HorizontalAlignment="Left" />
        </telerik:HierarchicalDataTemplate>
     
        <telerik:HierarchicalDataTemplate x:Key="HeaderTemplateA" ItemsSource="{Binding}" ItemTemplate="{StaticResource TemplateA}">
            <TextBlock Text="Header2" HorizontalAlignment="Left" />
        </telerik:HierarchicalDataTemplate>
     
        <selectors:ABSelector x:Key="ABSelector" BTemplate="{StaticResource HeaderTemplateB}" ATemplate="{StaticResource HeaderTemplateA}" />
     
        <telerik:HierarchicalDataTemplate x:Key="TemplateA" ItemsSource="{Binding CombinedList}" ItemTemplateSelector="{StaticResource ABSelector}">
            <TextBlock Text="{Binding something}" />
        </telerik:HierarchicalDataTemplate>
    </UserControl.Resources>
     
    <Grid>
          <telerik:RadTreeView ItemsSource="{Binding TypeAList}" ItemTemplate="{StaticResource TemplateA}" />
    </Grid>

    But it didn't work, because TemplateA uses ABSelector, ABSelector uses HeaderTemplateA and HeaderTemplateA uses TemplateA. I'm stuck in this circular dependency among these resources. And, I couldn't figure out another way.
    Am I missing some point, or is there any other way to handle this problem.

    Thank you,
    Nurullah
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 10 Apr 2013 Link to this post

    Hello Nurullah,

    Please have in mind that the RadTreeView control displays hierarchical data. In your case, you need to display a flat collection (a List) in hierarchical data template, which is not supported out-of-the-box. You will have to create a hierarchical collection before using it.

    In order to achieve your requirements you have to create four HierarchicalDataTemplates (applied to the first four levels of your tree) and one DataTemplate (for the last level). By following this approach you will not need an ItemTemplateSelector.


    In case that you need to use an ItemTemplateSelector, you will have to define it in your tree, rather than in your HierarchicalDataTemplate. For your convenience I prepared a sample project demonstrating the usage of a ItemTemplateSelector. Please take a look at it and let me know if we can further assist you.

    Please note that I create my own hierarchical collection (Items) in order to use it in the RadTreeView control.

    Regards,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Bala
    Bala avatar
    20 posts
    Member since:
    Dec 2011

    Posted 16 Oct 2014 in reply to Pavel R. Pavlov Link to this post

    HI  Nurullah,

          i have a case where i need to find the last node(last Child) of the Item Type-A1-(i.e.-Type-B3). Is it possible to find it with DataItem from the abouve sample -TreeView-SL-Templates.zip .

           Type-A1
                      Header2
                          Type-A2
                                  Header1
                                        Type-B3

     

    With regards,
    Bala S

  5. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 20 Oct 2014 Link to this post

    Hello,

    If I understand you correctly you need to find which item nested deepest in your custom collection of business objects. If this is your requirement you can achieve it by monitoring the RadTreeViewItem.Level property. It is of type int and indicates the level of nesting of the items. For example in your reply the Type-A1 node is Level 0, the Header2 is Level 1, the Type-A2 is Level 3 and so on. 

    You can bind the RadTreeViewItem.Level property to a custom property exposed by your business item and then you will be able to traverse your collection and pick the item which is deepest nested.

    Regards,
    Pavel R. Pavlov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top