How to use the Silverlight DataTemplateSelector?

Topics: Prism v4 - Silverlight 4
Jan 9, 2012 at 1:50 AM


I have a view model that exposes a master-detail scenario. The property that exposes the 'detail' in the VM, wraps it in a new VM that has two states Normal and Edit.

I want to create a DataTemplateSelector that switches between these states accordingly.

Note, I'm using SL5.


Jan 9, 2012 at 6:38 PM


As far as I know, the DataTemplateSelector class does not have support  out of the box for using values of properties as keys for its DataTemplates. As explained in MSDN, the DataTemplateSelector class determines the template that must be applied to its content retrieving it from its resources using the name for the type of the content as the key. This means that the key of each DataTemplate must be the name of a type.

Based on my understanding of your scenario, in order to use the DataTemplateSelector class to show the corresponding UI to each state, you would need to implement a view model class for the "Normal" state and one for the "Edit" state. For example, you could define a ShowDetailViewModel and EditDetailViewModel view model classes that inherit from a DetailViewModel base class. Then, you could define the Detail property of the master view model to return a DetailViewModel object type, and accordingly to the "state," return a ShowDetailViewModel (if the state is Normal) or a EditDetailViewModel (if the state is Edit). The resources of the DataTemplateSelector for this, could be something like the following:

<prism:DataTemplateSelector Content="{Binding Detail}">

        <DataTemplate x:Key="ShowDetailViewModel">

        <DataTemplate x:Key="EditDetailViewModel">


You can find more information regarding DataTemplateSelectors in the following blog post:

I hope you find this useful,

Damian Cherubini

Jan 10, 2012 at 1:47 AM

Hi Damian and thanks for your quick reply.

In my scenario I am developing a mini CRM app, where I didn't want to waste my time coding separate VMs for each scenario. Hence I decided to go for simple BooleanToVisibility switch based on the 'IsEditing' property on the VM.

Another alternative I considered FYI is the DataForm.

I agree that the DataTemplateSelector is very useful the way it is, but I think it would be even greater if some additional template-switching features will be added to it (boolean switching, enum switching, string switching, type switching - the common to all of these is there should be a common way to switch between templates. Having the type name string representation as type name is somewhat risky and error prone.

Thank you so much for your helpful post,

Jan 10, 2012 at 7:05 PM

Hi Shimmy,

If you believe that the DataTemplateSelector should be able to select the corresponding template based on other parameters (for example, the ones that you mentioned above) you could create a work item in the issue tracker as a suggestion so that the team might have it in consideration for future releases.


Damian Cherubini