ICollectionViewFactory might not get called unless it’s IEnumerable


I got lost in some circle of WPF hell this afternoon. Eventually I started wondering if using ICollectionViewFactory to instantiate a collection view would be the cure to all my problems. However, I found myself banging my head against brick walls just figuring out how to use ICollectionViewFactory.

In the process I noticed (again) that MSDN docs for interfaces are sometimes really quite useless. For instance, that an interface designed as an extensibility hook actually will, in some scenario, be called (to do the thing you want to do by overriding the interface) is important. And deserves a documentation of some sort. Especially for interfaces that are known to get called in some everyday scenario. Oh well.

Now for ICollectionViewFactory, it’s perfectly clear from the interface name what the interface is expected to do. From the name , you can even guess that it might be called by the default collection view manager which tries to create a default collection view for your collection. And a little reflection diving will confirm that. So far so good…

Just one problem, when you try to use a binding to set your ICollectionViewFactory to the ItemsSource on e.g. a DataGrid, it doesn’t get called. Why?

(Bash, bash, bash.)

Later, a thought happens: “Oh yeah, ItemsSource is of type IEnumerable….”

Yes indeed. This is the solution. My ICollectionViewFactory didn’t implement IEnumerable and dependency property type checking was preventing my ICollectionViewFactory being set as an ItemsSource. So of course my factory got created, but I never saw Create() called. Add a dummy implementation of IEnumerable, and it works. Yay… now if only that was my last problem.


Comments (0)

Skip to main content