More. DataTemplates. In. WPF.
Imagine you want to fill a toolbar through its ItemsSource and appropriate DataTemplates and not all commands are created equal. Some commands should be shown as Buttons, or ToggleButtons or whatever Visual tree you may come up with. In such a situation you can use a DataTemplateSelector. How to implement one (not exceedingly complicated) is described here. Alas, it requires you to leave the realms of XAML. One idea is then to have a XAML-friendly template selector. The idea isn’t new, one implementation can be found here. However I wanted something even simpler: A XAML-based template selector that is only driven by the DataType specified on the template. The API shown makes it possible to have the XAML look like this:
The DataTemplateChoice is a container for an unlimited number of DataTemplates. The implementation is rather simple:
Rather important for comfort is the specification of the ContentPropertyAttribute. That way the XAML compiler knows which property to target per default, thereby allowing to write straight into the DataTemplateChoice and ensuring that the content ends up in the Templates property.
Finally we need the DataTemplates class, which is pretty much just a list with a method to pick out a DataTemplate:
And that’s that! Make sure to specify the DataType on the DataTemplates you add. Via the StaticResourceExtension you can add DataTemplates specified elsewhere. The selection is similar to the catch blocks of a try: Move down from specific to more general. And finally, specifying an interface as DataType will work without problems due to usage of IsAssignableFrom.