本文介绍了TreeView和Entity Framework绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF和EF的新手;我看过,但是我找不到合适的帮助。






这是我所拥有的:



实体框架 ReportDefinition.ParentID ReportDefinition.SectionID Section.idSections ), ReportDefinition示例部分示例



这是我想介绍的:



TreeView








我试图实现这是以编程方式我非常感谢任何帮助。

解决方案

您需要一个顶级 ReportDefinition 对象:

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

您需要将此集合绑定到 ItemsSource TreeView



在EF中,您还需要创建一个父子 ReportDefinition 之间的关系使用 ParentID 链接到父级。为方便起见,您可以命名反向收藏儿童。然后收集 ReportDefinition 直接在另一个 ReportDefinition 之下的对象集合:

  ReportDefinition.Children 

然后你必须在 TreeView 中创建 HierarchicalTemplate

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


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


This is what I have:

Entity Framework (ReportDefinition.ParentID and ReportDefinition.SectionID are Section.idSections), ReportDefinition example and Section example.

This is what I would like to present:

TreeView.

.


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

解决方案

You need a collection of top-level ReportDefinition objects:

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

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

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

You then have to create HierarchicalTemplate in the TreeView:

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

这篇关于TreeView和Entity Framework绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 15:56