HierarchicalDataTemplate Binding Multiple Collections

5 posts, 0 answers
  1. Dave
    Dave avatar
    26 posts
    Member since:
    Jun 2009

    Posted 22 Jul 2009 Link to this post

    I am trying to come up with a way to bind, using HierarchicalDataTemplate, to multiple levels.

    Here is the situation.  I have a hierarchical object that I am binding to.  The object two levels deep has two collections I want to bind to and have each collection appear at the same level of the tree.  So my hierarchy splits in the middle. 

    Is it possisble to specify two ItemsSources and Two ItemTemplates?  Can I do it with a value converter?

    Thanks
    Dave
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 22 Jul 2009 Link to this post

    Hello Dave,

    Yes, you can try a value converter that will take the class with the two collections and return the union of them. This will sort out the hierarchy but then you may still need two templates. In this case, you can use the ItemTemplateSelector and return a different template based on some criteria (Type I imagine).

    A slightly better approach IMO will be to add a property to your class that returns the union of the two collections. This can be done easily with linq, you can return

    collection1.Union(collection2)

    If you are using a generated class (like from a WebService) you will find that these classes are generated as partial so that you can easily extend them for scenarios like this one.

    Unfortunately both these approaches will not deal with the case when the two collections are observable and you need to be notifies when objects are added/removed. If this is the case for you, I can reply with more details how this can be implemented.

    Kind regards,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Dave
    Dave avatar
    26 posts
    Member since:
    Jun 2009

    Posted 22 Jul 2009 Link to this post

    Thanks for the reply I will not be able to use the Union operation because the collection are of two different type and Union expects the the same types.  I will have to do a little more magic.  You have given me an idea on how to do what I want to do.  Thanks

    Dave
  4. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 22 Jul 2009 Link to this post

    Hello Dave,

    Yes, I missed the fact that union requires the same types. To follow up on this then you can cast the items to Object (or a common class) before doing the union like so:

    collection1.Cast<Object>().Union(collection2.Cast<Object>())

    you can of course use a different approach as you see fit.

    Sincerely yours,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Rav Panchalingam
    Rav Panchalingam avatar
    2 posts
    Member since:
    Oct 2009

    Posted 07 Apr 2010 Link to this post

    Hi Dave,

    Were you successful in using ItemTemplateSelector for this? Could you post an example of how to acheive the tree split, as I am facing a similar problem.

    Rav
Back to Top