开发者

Setting up a TreeView in Silverlight with a HierarchicalDataTemplate and Binding with ItemSource

开发者 https://www.devze.com 2023-02-16 02:48 出处:网络
I am trying to build a TreeView and have it set up like this link: Silverlight vs WPF - Treeview with HierarchialDataTemplate

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

- - 开发者_Go百科- - 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


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

精彩评论

暂无评论...
验证码 换一张
取 消