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

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

问题描述

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



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



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



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

解决方案

我已经从Abe Heidebrecht 。非常感谢他。



这是我的XAML ...

  < Window x:Class =Window1
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http:// schemas。
xmlns:local =clr-namespace:PageManager
Title =Window1Height =300Width =300Name =Window1> ;
< Grid>
< TreeView Margin =12Name =TreeViewPagesItemsSource ={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代码...



< pre class =lang-vb prettyprint-override> Class Window1

Private Sub Window1_Loaded(ByVal sender As System.Object,ByVal e As System.Windows.RoutedEventArgs)处理MyBase .Loaded
Dim db As New PageEntities
Dim RootPage = From p在db.Page.Include(Children)_
其中(p.Parent is Nothing)_
选择p
TreeViewPages.ItemsSource = RootPage
End Sub

Private Sub TreeViewPages_Expanded(ByVal sender As System.Object,ByVal e As System.Windows.RoutedEventArgs)
Dim ExpandedTreeViewItem作为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)_
其中p.Parent.Id = PageId _
选择p
ExpandedTreeViewItem.ItemsSource = ChildPages
End Sub
结束类

当窗口加载时,根节点及其子进程将从数据库中查询并插入到树中。



每次展开一个节点时,会从数据库中查询该节点的子孙,并将其插入到树中。


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.

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

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.

How do I display this hierarchy in a WPF TreeView?

解决方案

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.

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

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