0

I am trying to build a TreeView and have it set up like this link:

Silverlight vs WPF - Treeview with HierarchialDataTemplate

As a comment to the first actual answer provided the poster says how they solved it but they didn't provide code and I understand what they said but I am really new to this and can't get it right. I have the same set-up structure with the entities and groups. I was wondering if some one could explain what the xaml ended up looking like. I am assuming by saying they made a new node class it just means they made a class that contains essentially a list of groups. Something like

class groupHolder
{
   public List<Group> myGroups {get;set;}
   public groupHolder() { myGroups = new List<Group>(); } 
}

I am just trying to go three levels deep:

Group 1
- - - - AnotherGroup1
- - - - - - - - entity1
- - - - - - - - entity2
- - - - AnotherGroup2
- - - - - - - - entity1
Group2
- - - - Entity1
- - - - Entity2
- - - - AnotherGroup1
- - - - - - - - entity1
- - - - - - - - entity2
- - - - AnotherGroup2
- - - - - - - - entity1
and so on...

Like I said, I am new to this. I've also been trying to use this tutorial:

http://blogs.microsoft.co.il/blogs/davids/archive/2009/06/04/hierarchicaldatatemplate-and-treeview.aspx

but when I try and set another HierarchicalDataTemplate it says that ItemTemplate is set more than once. I'm lost.


edit: found this link on the web, it helps, too.... I think...

http://www.codeproject.com/Articles/36451/Organizing-Heterogeneous-Data-on-a-WPF-TreeView.aspx

Community
  • 1
  • 1
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59

1 Answers1

4

I was able to re-create that structure:

TreeView:

 <sdk:TreeView Grid.Row="2"                                            
                      ItemTemplate="{StaticResource GroupTemplate}"
                      ItemsSource="{Binding Path=Groups}">            
        </sdk:TreeView>

Templates:

 <UserControl.Resources>       
        <common:HierarchicalDataTemplate x:Key="EntryTemplate">
            <TextBlock Text="{Binding Path=Name}" />
        </common:HierarchicalDataTemplate>
        <common:HierarchicalDataTemplate x:Key="SubGroupTemplate"
                                         ItemsSource="{Binding Path=Entries}"
                                         ItemTemplate="{StaticResource EntryTemplate}">
            <TextBlock Text="{Binding Path=Name}" />
        </common:HierarchicalDataTemplate>
        <common:HierarchicalDataTemplate x:Key="GroupTemplate"
                                         ItemsSource="{Binding Path=SubGroups}"
                                         ItemTemplate="{StaticResource SubGroupTemplate}">
            <TextBlock Text="{Binding Path=Name}" />
        </common:HierarchicalDataTemplate>
    </UserControl.Resources>

In the ViewModel I have:

public List<Group> Groups { get; set; }

Rest:

 public class Group
    {
        public int Key { get; set; }
        public string Name { get; set; }
        public List<Group> SubGroups { get; set; }
        public List<Entry> Entries { get; set; }
    }

  public class Entry
    {
        public int Key { get; set; }
        public string Name { get; set; }
    }
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
  • Ooo, thank you. Let me play with it for a minute and I'll get back to you. Thank you so much, I have no idea why this was so had for me to understand. – gloomy.penguin Mar 11 '11 at 19:51
  • I had to do something similar and it took me a bit also. Once I figured out how setting the templates ItemSource worked, it made sense. – Derek Beattie Mar 11 '11 at 19:59