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

Recursive template?

4 Answers 203 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 25 Mar 2009, 09:09 PM
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;


4 Answers, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 26 Mar 2009, 11:31 AM
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.
0
James
Top achievements
Rank 1
answered on 26 Mar 2009, 12:49 PM
Yes - that's fixed it :-)

Thanks for your help, James.
0
Paco
Top achievements
Rank 1
answered on 20 Jul 2011, 02:13 PM
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

0
Petar Mladenov
Telerik team
answered on 25 Jul 2011, 01:44 PM
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!

Tags
TreeView
Asked by
James
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
James
Top achievements
Rank 1
Paco
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or