*Note – Class Definition and sample data used in this example are provided in this previous blog post.
Template Selectors allow you to switch the Data Template used on an item being bound based on some logic. For instance, in a banking application, you may wish an account that has a negative balance to be highlighted with a red background in order to draw the attention of the user. Other positive balance accounts can be rendered using a different, milder looking template. To implement a Template Selector, use inheritance through extending the System.Windows.Controls.DataTemplateSelector and overriding the SelectTemplate method. The signature of the method is as follows:
In this case, item is the actual object being bound. You can analyze the properties of the object to determine which template to use when rendering. In this example, the data template selector will be performing logic on the ImageFavorite class to determine which template to use. Two data templates have been defined in resources, one being a summary view defined by the SimpleTemplate key, and a more detailed view defined by the DetailedTemplate key. The logic of the RatedImageTemplateSelector is if the item [ImageFavorite] being bound has a 5 star rating, utilize the detailed template, otherwise have it render using the simple template. The listing for the RatedImageTemplateSelector is as follows:
In order to utilize the template selector, you need to first declare its namespace in the window tag of the xaml file where it is being used. In this case the namespace is TemplateSelector which resides in the current project assembly:
Then define an instance of the template selector in resources:
This instance of the RatedImageTemplateSelector can now be used by the list box rendering ImageFavorite items by assigning it to the ItemTemplateSelector attribute:
Screen shot of two different data templates being rendered in a list box based on logic defined in a template selector:
Sample code for this example can be found here