WPF oftenly provides you with an option to set an ItemTemplate or an ItemTemplateSelector when styling say lists or even content controls (ContentTemplate). This is useful although most would probably just define an implicit style by not giving it a key:

This is all fine and even works if you’ve got multiple different items in a list. The problem arises when you want to style an interface of a view model. In lack of a better way around it I usually end up creating an interface for my view models using an implementation for production and one for design time. But, WPF can’t handle implicit data templates for interfaces. So doing this wouldn’t work:

Why not? We’ll it’s a design choice made by Microsoft since a type might implement multiple interfaces, what if an item had both IViewModelOne and IViewModelTwo? Instead of throwing an exception or something MS has decided to just not support this behaviour. So the way to get around this is to use a TemplateSelector. I don’t really like to rely on code behind code too much though, not for the sake of it being code but because it makes the styling less declarative.

To get around this I’ve written a small template class that allows you to define the templates and bind them to types in XAML instead. So this is how it looks:

And this is the class(es):

This should be simple to convert to a similar style selector as well if that’s needed. Just a snippet to help you along the way.

Cheers