Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeView and Entity Framework binding

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.

Diagram Picture.


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

like image 886
Yeseanul Avatar asked Jun 29 '26 02:06

Yeseanul


1 Answers

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>
like image 76
Martin Liversage Avatar answered Jun 30 '26 15:06

Martin Liversage