Wpf – How to use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities

data-bindingentity-frameworklinqwpfxaml

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?

Best Answer

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.