Binding self-referencing and additional table to TreeView

2 posts, 0 answers
  1. Kevin
    Kevin avatar
    1 posts
    Member since:
    Sep 2008

    Posted 01 Jul 2011 Link to this post

    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,
  2. Petar Mladenov
    Petar Mladenov avatar
    2882 posts

    Posted 06 Jul 2011 Link to this post

    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.

    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!

  3. DevCraft R3 2016 release webinar banner
Back to Top