Multiple Child Collections

4 posts, 1 answers
  1. David Brubacher
    David Brubacher avatar
    29 posts
    Member since:
    Mar 2010

    Posted 26 May 2015 Link to this post

    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?

  2. Answer
    Martin
    Admin
    Martin avatar
    1099 posts

    Posted 27 May 2015 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. David Brubacher
    David Brubacher avatar
    29 posts
    Member since:
    Mar 2010

    Posted 27 May 2015 in reply to Martin Link to this post

    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...

  5. David Brubacher
    David Brubacher avatar
    29 posts
    Member since:
    Mar 2010

    Posted 27 May 2015 in reply to David Brubacher Link to this post

    Oops - not your last paragraph - I meant your code comments about ProductBase
Back to Top