How to display two ObservableCollections as a single list in WPF?
- by nareshbhatia
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. 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!