Using the ObservableCollection<T> collection is a very common practice among UI developers. Added to the fact that they allow easy UI updates, they also allow sorting and filtering operations via CollectionViews. This post describes a pattern linking an ObservableCollection to sortable and filterable ListCollectionViews, which are themselves displayed in the UI.
The pattern works as follows:
1. Define a get/set property for the source ObservableCollection, and a read-only property for each view of the source
2. When the source ObservableCollection is set :
a. Instanciate the ListCollectionViews
b. On each one of them add if necessary:
i. SortDescription(s) for views that involve sorting
ii. Filter predicates for views that filter
c. Raise the PropertyChanged event on the source ObservableCollection property and on each view.
3. In your XAML UI code, bind to the ListCollectionView properties
The views are automatically updated in regards to the source’s composition (as defined by NotifyCollectionChangedAction). Changes to a property of an element contained within an ObservableCollection will not trigger an update of the views. This functionality, as described in a previous post, can be implemented using another type of Collection which we’ll explore in further details in the next post.
Some more details
CollectionViews separate the presentation of a Collection, from the source collection itself. This allows (for example) scenarios where an ObservableCollection can be filtered and sorted on screen, without having to modify the source Collection or having to handle temporary collections which would have to be kept in sync with the source.