How can I utilize LINQ to Entities with a WPF TreeView HierarchicalDataTemplate?

data-binding entity-framework linq wpf xaml

Question

My.edmx ADO.NET Entity Data Model file has a Page class with Parent and Children attributes. A hierarchy of Pages is intended.

- ADO.NET Entity Framework Hierarchical Page Class dead ImageShack link deleted

A ParentId foreign key in the Page table that is tied to the Id primary key of the same Page table manages this in my SQL database.

How can I make a WPF TreeView show this hierarchy?

1
14
8/24/2015 5:05:02 PM

Accepted Answer

I was able to do this with the aid of Heidebrecht, Abe. I'm grateful to him.

I have my XAML here.

<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>

My Visual Basic code is below.

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

The root node and its children are retrieved from the database and added to the tree as soon as the window loads.

When a node is enlarged, the database is searched to find the node's children and grandchildren, and they are then added to the tree.

16
5/23/2017 11:54:25 AM

Popular Answer

As an alternative: (well, very similar, but slightly different)

Your window load function should:

PageEntities db = new PageEntities();
TreeViewPages.ItemsSource = db.Page.Where(u=>u.Parent==null);

Make a fresh copy of Page.cs.

public partial class Page {
    public ObjectQuery<Page> LoadedChildren {
        get {
            var ret = Children;
            if(ret.IsLoaded==false) ret.Load();
            return ret;
        }
    }
}

Within your XAML:

<TreeView Name="TreeViewPages">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemSource="{Binding LoadedChildren}">
            <TextBlock Text="{Binding ShortTitle}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

You should get the gist even if it hasn't been tested.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow