Recursive template?

5 posts, 0 answers
  1. James
    James avatar
    117 posts
    Member since:
    Mar 2009

    Posted 25 Mar 2009 Link to this post

    I have a treeview that can be of variable depth and any depth. It's up to the user and we can't tell (without lots of coding) how deep any one node will go. I was hoping to be able to databind the tree in one go, but have hit a problem.

    I'm loading a set of objects up in codebehind and binding them to the treeview:

    TreeView.ItemTemplate = ((DataTemplate)this.Resources["CodeSet"]);
    TreeView.ItemsSource = e2.Object;

    So far so good, this works fine and I get my top level of root nodes.

    Each 'CodeSet' object contains an object 'Attributes'. My templates look like this:

    <core:HierarchicalDataTemplate x:Key="AttributeTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding AttributeName}" Foreground="Black" FontWeight="Bold" FontSize="15" />
                </StackPanel>
            </core:HierarchicalDataTemplate>
            
            <core:HierarchicalDataTemplate x:Key="CodeSet" ItemTemplate="{StaticResource AttributeTemplate}" ItemsSource = "{Binding Attributes}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding SetName}" Foreground="Black" FontWeight="Bold" FontSize="15" />
                </StackPanel>
            </core:HierarchicalDataTemplate>

    Again, so far, so good - I get my top level of nodes and the next level down. (Interestingly, if I have the AttributeTemplate declared after the CodeSet template I get the error "Cannot find a Resource with the Name/Key AttributeTemplate".)

    Now for the problem. Each 'Attributes' object contains an 'attributes' object which either contains more attributes or is empty. In order to get this working, I thought I should add an ItemTemplate and ItemsSource to the AttributeTemplate making my final code look like this (essentially AttributeTemplate should keep being called until we get to the lowest node):

    <core:HierarchicalDataTemplate x:Key="AttributeTemplate" ItemTemplate="{StaticResource AttributeTemplate}" ItemsSource = "{Binding Attributes}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding AttributeName}" Foreground="Black" FontWeight="Bold" FontSize="15" />
                </StackPanel>
            </core:HierarchicalDataTemplate>
            
            <core:HierarchicalDataTemplate x:Key="CodeSet" ItemTemplate="{StaticResource AttributeTemplate}" ItemsSource = "{Binding Attributes}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding SetName}" Foreground="Black" FontWeight="Bold" FontSize="15" />
                </StackPanel>
            </core:HierarchicalDataTemplate>

    But it doesn't work. I get the error "Cannot find a Resource with the Name/Key AttributeTemplate". Any suggestions to fix this gratefully received!





    TreeView.ItemsSource = e2.Object;


  2. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 26 Mar 2009 Link to this post

    Hello James,

    There is no need to set the ItemTemplate property of your AttributeTemplate - if you do not set it, then the same template is assumed for any child items. Please try that and if you still have problems, please send me your sample project so that I can take a look.

    All the best,
    Tihomir Petkov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. James
    James avatar
    117 posts
    Member since:
    Mar 2009

    Posted 26 Mar 2009 Link to this post

    Yes - that's fixed it :-)

    Thanks for your help, James.
  5. Paco
    Paco avatar
    2 posts
    Member since:
    May 2011

    Posted 20 Jul 2011 Link to this post

    Hi,
    we are using HierarchicalDataTemplate as follow:

    <telerik:HierarchicalDataTemplate x:Key="TreeViewItemTemplate" ItemsSource="{Binding Path=Children}">

    <StackPanel Orientation="Horizontal">

    <TextBlock Text="{Binding Path=DescripcionArticulo}" FontSize="8" FontWeight="Normal"/>

    </StackPanel>

    </telerik:HierarchicalDataTemplate>

    Then we dynamicaly create a tree in C# like this:

    RadTreeView tree = new RadTreeView();

    tree.ItemTemplate = this.Resources["TreeViewItemTemplate"] as Telerik.Windows.Controls.HierarchicalDataTemplate;

    tree.Selected += new EventHandler<RadRoutedEventArgs>(this.RadTreeViewItem_Selected);

    ObservableCollection<treeProductoViewModel> listaItems = new ObservableCollection<treeProductoViewModel>();

    listaItems = generarLista(data, data[i][1]);

    tree.ItemsSource = listaItems;

    and get only 2 levels. Can you help? Thanks

  6. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 25 Jul 2011 Link to this post

    Hi Paco,

    We replied to your question in this forum thread.

    Kind regards,
    Petar Mladenov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top
DevCraft banner