TreeView Binding to multiple Object types in MVVM

5 posts, 0 answers
  1. Rajat Panwar
    Rajat Panwar avatar
    11 posts
    Member since:
    Aug 2009

    Posted 27 Sep 2012 Link to this post

    Hello,



    I have an Accounts class which has various collections inside it like Users, Groups, Orders etc. I want to bind a treeview to Accounts class so that when I expand Accounts. It should have users, groups, orders etc. items.



    The treeview should look like this

    +ACCOUNT

        +USERS

            USER1

            USER2

            USER3

        +GROUPS

            GROUP1

            GROUP2

            GROUP3

        +ORDERS

            ORDER1

            ORDER2

            ORDER3



    It will be lot helpful if you guys could help me with some sample code.



    Thanks
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 02 Oct 2012 Link to this post

    Hi Rajat,

    The control you need in this case is RadTreeView. It can be bound to a hierarchical data (such as yours) using a custom DataTemplate. You have to use telerik:HierarcichalDataTemplate in order to customize it. You can use different data binding approaches, to achieve your goal, as described in this article. If you need some sample projects you can take a look through our code library.

    Regards,
    TeamX2_Pavel
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Rajat Panwar
    Rajat Panwar avatar
    11 posts
    Member since:
    Aug 2009

    Posted 08 Oct 2012 Link to this post

    Hello,

    I guess you have not read my Question Completely. I know I have to use RadTreeView as I have already mention in my question that I want to bind a treeview.

     

    I'll explain my question again. I'll take example of this sample. Here RadTreeView is having itemsource which is collection of class MyViewModel. Now this class has only one collection in it which is RelatedItems.

    In my scenario. I would like to have multiple collections inside like :

     

    public class  Account  
    {
      
    public IList<User> Users  
        
            get
            set
        
      
    public IList<Group> Groups  
        
            get
            set
        }
    public IList<Order> Orders  
        
            get
            set
        }
      
    }

    Now How can I bind a RadTreeView to Collection of Account class so that It can display all users, groups, orders collection
    .

    Thanks and Regards
    Rajat Panwar
  5. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 11 Oct 2012 Link to this post

    Hi Rajat,

    Thank you for giving us additional information about your scenario. Now we can better understand it and a good option for implementing it is to use a Converter to convert your data into the  hierarchy you need to display in the RadTreeView

    This scenario is specific because you have three different collections in the Account object and the HierarchicalDataTemplate can display only one collection. This is why you'll need a Converter to create a new ObservableCollection populated with your data (IList<Group> Groups, IList<Order> Orders and IList<User> Users). In addition to this you need to display these collections as "grand-children" of an Account node so you'll have to make sure that the Converter will return a collection of hierarchical data.

    And as each collection in the Account item is of different type, you can use an ItemTemplateSelector to apply a different template on the different types of nodes.

    Please take a look at the attached project and see if it works for you. Don`t hesitate to ask if you have any questions.

    All the best,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Rajat Panwar
    Rajat Panwar avatar
    11 posts
    Member since:
    Aug 2009

    Posted 21 Oct 2012 Link to this post

    Hello Pavlov,

    Thanks a lot for your sample. This was exactly what I wanted.

    Thanks and Regards,

    Rajat Panwar 

Back to Top
DevCraft banner