问题描述
我是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绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!