I have two ObservableCollections, say ObservableCollection<Cat>
and ObservableCollections<Dog>
. Cat and Dog both derive from class Pet. I want to display a list of all Pets开发者_如何学Python. How do I do this? I prefer not want create a new ObservableCollection<Pet>
by adding items from the two source lists because this list will become stale as more Cats and Dogs are added to the source lists. I can think of two approaches:
1) Create a "Decorator" ObservableCollection
that keeps the two source collections as members and iterates over them every time.
2) Create an ObservableCollection<Pet>
that does have the combined elements of the two source collections, but is also dependent on the source collections. Thus if a Cat is added to the Cat collection, this collection is notified and it adds the new Cat to itself.
Is there a standard way to solve this issue? I don't want to reinvent the wheel!
Use a CompositeCollection to aggregate multiple collections and items with full collection change support.
Edit: CompositeCollection
isn't a dependency object so there no notion of data context, hence why the binding doesn't work. You have to create the collection from code behind if you add bound items or collections.
精彩评论