I've got a Page class in my .edmx ADO.NET Entity Data Model file with with Parent and Children properties. It's for a hierarchy of Pages.
removed dead ImageShack link - ADO.NET Entity Framework Hierarchical Page Class
This is handled in my SQL database with a ParentId foreign key in the Page table bound to the Id primary key of that same Page table.
How do I display this hierarchy in a WPF TreeView?
I got this working with help from Abe Heidebrecht. Much thanks to him.
Here's my XAML...
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PageManager"
Title="Window1" Height="300" Width="300" Name="Window1">
<Grid>
<TreeView Margin="12" Name="TreeViewPages" ItemsSource="{Binding}" TreeViewItem.Expanded="TreeViewPages_Expanded">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Page}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Path=ShortTitle}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
Here's my Visual Basic code...
Class Window1
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim db As New PageEntities
Dim RootPage = From p In db.Page.Include("Children") _
Where (p.Parent Is Nothing) _
Select p
TreeViewPages.ItemsSource = RootPage
End Sub
Private Sub TreeViewPages_Expanded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim ExpandedTreeViewItem As TreeViewItem = DirectCast(e.OriginalSource, TreeViewItem)
Dim PageId As Guid = DirectCast(ExpandedTreeViewItem.DataContext, Page).Id
Dim db As New PageEntities
Dim ChildPages = From p In db.Page.Include("Children") _
Where p.Parent.Id = PageId _
Select p
ExpandedTreeViewItem.ItemsSource = ChildPages
End Sub
End Class
When the window loads, the root node and its children are queried from the database and inserted into the tree.
Each time a node is expanded, that node's children and grandchildren are queried from the database and inserted into the tree.
A different way: (well, very similar, but slightly different)
In your Window Load function:
PageEntities db = new PageEntities();
TreeViewPages.ItemsSource = db.Page.Where(u=>u.Parent==null);
Create a new file Page.cs
public partial class Page {
public ObjectQuery<Page> LoadedChildren {
get {
var ret = Children;
if(ret.IsLoaded==false) ret.Load();
return ret;
}
}
}
In your XAML:
<TreeView Name="TreeViewPages">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemSource="{Binding LoadedChildren}">
<TextBlock Text="{Binding ShortTitle}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Its not tested, but you should get the general idea.