Tuesday, May 27, 2008

How do I provide custom collection view for collection in WPF?

Some time ago, while "surfing" the WindowsBase.dll with Reflector, I have found one interesting interface: ICollectionViewFactory.
I made some digging, and found out that there are folks that are interested what is this interface doing (along with some other interesting interfaces...)
MSDN says nothing about this interface, but it turned out that it could be quite useful in fact.
Here is how it is declared:

///

An interface that enables implementing collections to create a view to their data. Normally, user code does not call methods on this interface.
public interface ICollectionViewFactory
{
/// Creates a new view on the collection that implements this interface. Typically, user code does not call this method.
/// The newly created view.
ICollectionView CreateView();
}

It turned out that when you try to create a default collection view for the collection(or when you provide your collection as an items source for the ItemsControl), databinding engine checks whether your collection implements ICollectionViewFactory interface, and if it does - your CreateView() method is called, so in case you want to implement some specific logic, like optimized grouping, or specific filtering - you just implement your own collection view and return it in CreateView() method.

1 comment:

Oskar said...

Thanks for this post!
Just what I was looking for - a way to provide my custom collection view as the default view for my collections!