WPF TreeView HierarchicalDataTemplate - binding to object with different child collections

前端 未结 3 845
你的背包
你的背包 2021-01-14 13:33

I am trying to bind a collection to wpf TreeView control using data templates. Each item(Person) in the collection also contains two different collections(Cars,

3条回答
  •  甜味超标
    2021-01-14 14:23

    CompositeCollection is a good answer, except you can't bind the child collections to the DataContext because it's not a Freezable. You can only bind them to resources. (See this question for more on that.) That makes it kind of hard to use in a HierarchicalDataTemplate, since the ItemsSource in one needs to bind to a property in the current context to be useful.

    If you don't need change notification on the collections, you can easily implement a property in your view model, e.g.:

    public IEnumerable Items
    {
       get { return Books.Concat(Cars); }
    }
    
    
    

    If you need change notification on the collection, it's a lot less trivial.

    提交回复
    热议问题