如何在 LINQ to Entities 中使用 WPF TreeView HierarchicalDataTemplate? [英] How do I use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities?

查看:22
本文介绍了如何在 LINQ to Entities 中使用 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 Entity Framework Hierarchical Page Class

这是在我的 SQL 数据库中处理的,Page 表中的 ParentId 外键绑定到同一个 Page 表的 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 Entities 中使用 WPF TreeView HierarchicalDataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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