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

TreeView Binding to multiple Object types in MVVM

4 Answers 217 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rajat Panwar
Top achievements
Rank 1
Rajat Panwar asked on 27 Sep 2012, 06:27 PM
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

4 Answers, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 02 Oct 2012, 11:47 AM
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.

0
Rajat Panwar
Top achievements
Rank 1
answered on 08 Oct 2012, 09:16 AM

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
0
Pavel R. Pavlov
Telerik team
answered on 11 Oct 2012, 12:34 PM
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.

0
Rajat Panwar
Top achievements
Rank 1
answered on 21 Oct 2012, 10:20 AM

Hello Pavlov,

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

Thanks and Regards,

Rajat Panwar 

Tags
TreeView
Asked by
Rajat Panwar
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Rajat Panwar
Top achievements
Rank 1
Share this question
or