Here is my first post on what I will try and turn into a series of WPF/Silverlight differences and how to get around them. First I will begin by talking about DataTemplateSelector
.
I suppose before I talk about the DataTemplateSelector
class, I should mention a little bit about what a Template is. Unless you are new to WPF and Silverlight Development (and by extension, WP7 development) you will be aware all controls have a Template Property. This property is of type ControlTemplate
, and allows you to change the look and behaviour of a control, without affecting it’s behaviour. ContentControl which derives from Control has a Content field which holds an object. This object gets its look and feel from somewhere, and if it is a UIElement, then it comes from itself. But if the object is some other type, such as a POCO or ViewModel object, then the default behaviour is to use the object’s ToString()
method. Most of you have probably seen this numerous times and this is where the DataTemplate comes in.
ContentControl has another property called ContentTemplate
, of type DataTemplate
, that will be used in place of the content. By default, the DataContext of the Template will be the Content property of the ContentControl. The benefit of this is that we can apply Bindings to get at the properties of the object itself. This finally brings me to the differences section of this post, The DataTemplateSelector
. Up to this point, we are describing both Silverlight and WPF, but the ContentTemplateSelector
Property of type DataTemplateSelector
only exists on the WPF version of the ContentControl
Class. So before I tell you how to overcome this, I will convince you of why you would bother using this feature anyway.
DataTemplateSelector
is a nice little class that has an overridable method called SelectTemplate which takes an item(the Content object) and a container(the Data-Bound Object, in this case the ContentControl) and returns a DataTemplate. By overriding this class we can create some logic that determines which DataTemplate is used based on some conditions at the time of it’s use. This could be a property of the item, such as an enum or boolean field, the type of the object, or something more complex. Basically you write the logic and return the right DataTemplate.
For a simple example of how this works, check out this page which demonstrates using text and image file names for selecting the DataTemplate. how-to-use-a-datatemplateselector
Its also worth mentioning that the ItemsControl class (and therefore it’s derived types such as ListBox) also have an ItemTemplateSelector
which is a DataTemplateSelector
, and can be used to give a different dataTemplate to different items in a collection, bound or set to the ItemsControl’s Items.
So I’m sure you can start to see some places where this might be useful, but what about Silverlight? Fortunately there is a solution. Converters.
By using some little tricks, we can use a Converter and a ContentControl and bind our object that needs a template to the Content Property of our ContentControl using a DataTemplateSelectorConverter
.
For a simple implementation of the DataTemplateSelectorConverter
, check out this posting. Data Template Selector for Silverlight
I have made a few adjustments to this design, but I will save that for a later post. For now, this is one difference determined, and luckily a solution found. I will try to add a real example of using the selector for both Silverlight and WPF in a future post.