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]
# 1
Are you using a HierarchicalDataTemplate?
Martin_Moser at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

I am using the HierarchicalDataTemplate, although I am not sure if I am using it correctly since I am binding to the same object (Self referencing table).

JArnott at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3
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

ZhouYong at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

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.

SamBent-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

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
JArnott at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified