none
Treeview - Binding to self referencing table RRS feed

  • Question

  • I have two datatables, Folder and Item in a DataSet. Two relations are defined for the DataSet:
              
    dataSet.Relations.Add("FolderToFolder",
                    dataSet.Tables["Folder"].Columns["Id"],
                    dataSet.Tables["Folder"].Columns["ParentFolder"]);
              
               
    dataSet.Relations.Add("ItemToFolder",
                        dataSet.Tables["Folder"].Columns["Id"],
                        dataSet.Tables["Item"].Columns["FolderId"]);

    I want to create a treeview like this:

    Folder1
           Folder1:1
                 ItemA
                 ItemB
                 Folder1:1:1
                          ItemC
           Folder1:2
                 ItemD
           ItemE

    i.e. a folder can have other (sub)folders as well as items as children

    I have tried to solve the binding with HierarchicalDataTemplates but I'm not able to add both folders and items to the tree. Either I get the root folder and all items under it ore or the root folder and all folders under it.

    <HierarchicalDataTemplate x:Key="FolderDataTemplate" ItemsSource="{Binding FolderToFolder}" >
            <TextBlock Text="{Binding Label}"/>
    </HierarchicalDataTemplate>
                         
    <TreeView Name="treeView1" ItemTemplate="{StaticResource FolderDataTemplate}"/>

    I set the ItemSource in the .cs file

    treeView1.ItemsSource = folderItemView

    Any suggestions about how this could be done?

    Thanks
    Jonatan

    Thursday, April 16, 2009 7:40 AM

Answers