+6  A: 

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.

Zack Peterson
+3  A: 

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>
        <HierarchialDataTemplate ItemSource="{Binding LoadedChildren}">
            <TextBlock Text="{Binding ShortTitle}" />
        </HierarchialDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Its not tested, but you should get the general idea.

Mike Christiansen
This method loads the entire hierarchy all at once. The previously accepted answer is on-demand loading of child nodes.
Mike Christiansen
A: 

Not tested not even correctly typed in:

HierarchialDataTemplate -> HierarchicalDataTemplate

mehran