c# - TreeView 和 Entity Framework 绑定(bind)

标签 c# .net wpf linq entity-framework

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


这是我的:

Entity Framework (ReportDefinition.ParentIDReportDefinition.SectionIDSection.idSections), ReportDefinition 示例Section 示例

这是我想介绍的:

TreeView

Diagram Picture .


我正在尝试以编程方式实现这一点。如果有任何帮助,我将不胜感激。

最佳答案

您需要顶级 ReportDefinition 对象的集合:

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

您需要将此集合绑定(bind)到 TreeViewItemsSource

在 EF 中,您还需要在 ReportDefinition 上创建父子关系,使用 ParentID 将子项链接到父项。为方便起见,您可以将反向集合命名为 Children。紧接在另一个 ReportDefinition 之下的 ReportDefinition 对象的集合是这样的集合:

ReportDefinition.Children

然后您必须在 TreeView 中创建 HierarchicalTemplate:

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

关于c# - TreeView 和 Entity Framework 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9481799/

相关文章:

c# - Winform 中的 Int 字段不会使用 C# 更新 SQL Server 数据库中的空值

mysql - docker compose up后无法连接到mysql db

c# - Storyboard /动画的 "handful"影响性能?

wpf - 如何在WPF网格中获取所选单元格的行索引

wpf - 在 WPF 中创建自定义设计时集合编辑器

c# - 无法读取 dotnet 核心中链接的 appsettings.json 文件中的值

c# - 如何在xml中添加属性值

c# - 属性不适用于模型 ASP.NET MVC 5

c# - xaml.cs 中连续的 2 个括号是什么意思?

c# - 为 mac 制作一个自包含的可执行文件