This is a migrated thread and some comments may be shown as answers.

Binding self-referencing and additional table to TreeView

1 Answer 81 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 01 Jul 2011, 06:51 AM
I have two tables in a database (See attached photo EntityRelationship.png).  The table called Areas is self-referencing and the table called Items references Areas.  The data in each table is shown in AreaTableData.png and ItemTableData.png.  The hierarchy should look like this:

  • Area 1
    • Area 1 – 1
      • Item 1
      • Item 2
    • Area 1 – 2
      • Item 3
  • Area 2
    • Area 2 – 1
      • Item 5
      • Item 6
    • Area 2 – 2
    • Item 4

Note that Item 4 in the hierarchy is a child of Area 2.

I am attempting to bind this hierarchy to a RadTreeView.  I believe that I will need to utilize a HierarchicalDataTemplate, DataTemplateSelector and HierarchyConverter in order to make this happen, but I am unsure of how they should work together.  Any guidance would be greatly appreciated.

Thanks in advance,
Kevin

1 Answer, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 06 Jul 2011, 01:43 PM
Hello Kevin,

You can use ViewModels to wrap your objects from the Database. You can have AreaViewModel and ItemViewModel and they both could  implement Telerik.Windows.Controls.ViewModelBase interface. The AreaViewModel could have a child collection  - ObservableCollection<ViewModelbase> called Children. I attached a sample demonstrating how to use TemplateSelectors over such kind of hierarchy ( Folders that contain Folders and Files).
Now how to load the data in the ViewModels. You can use the technique called Lazy Loading. When needed, you will get your data from the database. For instance, when you need the Children collection of an Area you will populate it like so: iterate over the two tables and get those areas and items with AreaID == clickedArea.AreaId. You can check out this blog post showing a lazy loading example in the RadTreeView bound to HIerarchy of ViewModels. Please let us know if you need further assistance.

Regards,
Petar Mladenov
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
TreeView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or