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

Multiple Child Collections

4 Answers 266 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
David Brubacher
Top achievements
Rank 1
David Brubacher asked on 27 May 2015, 02:27 AM

I've used the example explaining how to create a windows explorer-like treeview, where a collection of children can contain both directories and files but I think I need a bit of clarification.

I'm in an MVVM scenario and my model classes contain multiple collections representing the different kinds of children they can contain. For example I might have grocery store object with a collection of dairy products, another collection of meats, etc. Is it correct that my view model should merge the collections into a single observable collection of objects (or something less general if possible) and then rely on the DataType={x:Type ....} to chose the right data template?

If that is the case and I wanted to keep my collection sorted, I would have to implement a custom sorter to group the dairy and meats prior to sorting, correct? Also, this should all still work if the children themselves can be containers for multiple types?

4 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 27 May 2015, 08:44 AM
Hi David,

Indeed, if you want to populate the tree with different types of business objects you will need to merge them in a single collection and pass them to the ItemsSource of the RadTreeView (or RadTreeViewItems, depending on the populated level). Here is an example for such structure based on your description:
public class GroceryStore
{
    public ObservableCollection<DairyProduct> DairyProducts { get; set; }
    public ObservableCollection<MeatProduct> MeatProducts { get; set; }
    public ObservableCollection<FruitProduct> FruitProducts { get; set; }
     
    public ObservableCollection<object> AllProducts
    {
        get
        {
            var result = new ObservableCollection<object>();
            // merge the products' collections into the result collection
            return result;
        }
    }
    // You can also consider using a base class for your products and use it instead of object
    // public ObservableCollection<ProductBase> AllProducts { get; }
}
About choosing the templates for the items you can use the treeview's ItemTemplateSelector as described in our help documentation. Basically, you can define different template for each business object and use a custom DataTemplateSelector that chooses the template based on the object's type (or any other condition).

As for the sorting, yes, the treeview doesn't have a sorting support out of the box and if you need such you can implement it with custom code in your view models. For example, you can sort the collection with all products before you pass it to the treeview.

I hope this information is useful.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
David Brubacher
Top achievements
Rank 1
answered on 27 May 2015, 01:18 PM

Thank you Martin

That confirms what I thought.

Regarding your last paragraph, the view models contain the observable child collections - not of the underlying classes but of the view models representing the child classes - so at the very least I could have ObservableCollection<ViewModelBase>.

 A better solution is to use an interface I think. Either that or extend HierarchicalDataTemplate so that ItemSource can take a collection of collections. Perhaps that's a feature request...

0
David Brubacher
Top achievements
Rank 1
answered on 27 May 2015, 01:20 PM
Oops - not your last paragraph - I meant your code comments about ProductBase
0
Jason
Top achievements
Rank 1
answered on 14 Feb 2017, 02:48 PM
I know this is an old question, but its still very relevant.  A great solution can be Phillip Sumi's blog (hardcodet.net) or on code project (https://www.codeproject.com/Articles/36451/Organizing-Heterogeneous-Data-on-a-WPF-TreeView).  It's very easy to implement and is exactly what you need.
Tags
TreeView
Asked by
David Brubacher
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
David Brubacher
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Share this question
or