Should I bind to ICollectionView or ObservableCollection
Solution 1:
You always bind to an ICollectionView
, whether you make it explicit or not.
Assume that we have
var collection = new ObservableCollection<string>();
var collectionView = CollectionViewSource.GetDefaultView(collection);
In this case, binding to collection
or to collectionView
is one and the same: the binding engine will bind to the default collection view (which is reference equal to collectionView
) if you tell it to bind to collection
.
This means that the answer to your question is "it makes absolutely no difference".
Just to be totally clear: even if you bind to the collection directly, the binding engine will bind to the default view. Modifying properties of the view such as sort criteria will affect the binding that appears to bind directly to the collection, since behind the covers it's a binding to the default view instead.
However, there is another interesting and related question: should one bind to the default collection view (i.e., to the collection itself, because there's no reason to explicitly bind to the default view) or to another view of the same collection?
Considering that each view has its own notion of current item, sort criteria, etc, it follows that if you intend to have multiple bindings to the same collection, and the bound controls need to have distinct notions of current item, filters and company, then what you want is to explicitly bind to multiple views of the same underlying collection.
Solution 2:
ObservableCollection<T>
implements INotifyCollectionChanged
and will notify the UI when the items in the collection have been changed.
ICollectionView
will give you the ability to filter, sort, or group the collection in addition to propogating INotifyCollectionChanged
events if the underlying collection implements it.
Either type works well with MVVM as long as you bind to it. Use ICollectionView
when you need sorting, filtering, or grouping. Use ObservableCollection<T>
directly when you don't.