如何使用具有LINQ to Entity的WPF TreeView HierarchicalDataTemplate? [英] How do I use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities?

查看:141
本文介绍了如何使用具有LINQ to Entity的WPF TreeView HierarchicalDataTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.edmx ADO.NET实体数据模型文件中有一个Page类,带有父和子属性。这是一个层次结构的页面。

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.

删除死亡ImageShack链接 - ADO.NET实体框架分层页面类

这是在我的SQL数据库中处理的,在Table表中的ParentId外键绑定到同一页表的Id主键。

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.

我如何在WPF TreeView中显示这个层次结构?

How do I display this hierarchy in a WPF TreeView?

推荐答案

我的工作与Abe Heidebrecht 。非常感谢他。

I got this working with help from Abe Heidebrecht. Much thanks to him.

这是我的XAML ...

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>

这是我的Visual Basic代码...

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.

这篇关于如何使用具有LINQ to Entity的WPF TreeView HierarchicalDataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆