How do you create a Hierarchical TreeView from a self referencing table?
For example I have a self referencing table such as
Pkparentname
10Inbox
20Deleted Items
31Inbox_subfolder
The problem that I am having is getting the subfolders to display correctly. I am getting a flat view rather than a Hierarchical view.
Any assistance would be much appreciated.
[987 byte] By [
JArnott] at [2008-1-10]
HierarchicalDataTemplate can work well with self referencing data structure as long as you use it the right way, I think you should show some piece of code of how you define the HierarchicalDataTemplate and exactly how do you retrieve the self referencing tables from the data source?
Sheva
TreeView and HierarchicalDataTemplate assume the data is already in hierarchical form. Meaning that there's a list of top-level objects, each of which has a property whose value is a list of its children, and so forth. In your case, this might look like:
top-level list: { Inbox, DeletedItems }
Inbox.Children: { Inbox_Subfolder }
DeletedItems.Children: { } (empty list)
You'll need to convert your table to a structure of this form. WPF doesn't have any built-in way to do this for you.
Thank you for your assistance, it happens that I was binding to a folder object in the HierarchicalDataTemplate
rather than the ChildFolders (Collection of childFolders). Code below resolved issue
<HierarchicalDataTemplate ItemsSource="{Binding Path=ChildFolders}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="4,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</Grid>
</HierarchicalDataTemplate>
Thanks
J Arnott