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;
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;