TreeView和实体框架结合 [英] TreeView and Entity Framework binding

查看:244
本文介绍了TreeView和实体框架结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的WPF和EF;我已经看了,但是我无法找到相应的帮助。

I'm new to WPF and EF; I've looked but I wasn't able to find appropriate help.

这是我有:

实体框架 ReportDefinition.ParentID ReportDefinition.SectionID 的是 Section.idSections 的), ReportDefinition例如栏目例如

这是什么,我想present:

This is what I would like to present:

的TreeView

.

我试图以编程方式实现这一目标。我更AP preciate任何帮助。

I'm trying to achieve this programmatically. I would much appreciate any help.

推荐答案

您需要的顶级集合 ReportDefinition 目标:

You need a collection of top-level ReportDefinition objects:

TopLevelReportDefinitions = ReportDefinitions.Where(rd => rd.ParentID == 0)

您需要在此集合绑定到的ItemsSource 的TreeView

You need to bind this collection to the ItemsSource of the TreeView.

在EF,你还需要创建 ReportDefinition 的父子关系,使用联孩子的父母 PARENTID 。为方便起见,你可以命名反向集合孩子。对 ReportDefinition 对象正下方另一个 ReportDefinition 的集合是那么集合:

In EF, you also need to create a parent-child relation on ReportDefinition linking the children to the parent using ParentID. For convenience you can name the reverse collection Children. The collection of ReportDefinition objects directly below another ReportDefinition is then the collection:

ReportDefinition.Children

您接下来要创建 HierarchicalTemplate 的TreeView

<TreeView ItemsSource="{Binding TopLevelReportDefinitions}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
      <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

这篇关于TreeView和实体框架结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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