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
2008-11-07 20:25:44