Getting by without a DataTemplateSelector in Silverlight

I was working on a Silverlight 4 project with a coworker a few days ago and needed to display a list of objects with different editors based on the type of object. The first thing that came to my mind was to use a DataTemplateSelector. I found out quickly that the Silverlight team – for whatever reason – decided they didn’t want to include a DataTemplateSelector. Frustrated, we turned to Google and found a blog post by Mike Gold that was basically exactly what we needed.  I made a few modifications to how he implemented this solution (which I think make it simpler), so I’m posting it here in case it might help someone else.

The idea is to use a custom IValueConverter to convert data templates into content inside of a ContentControl’s Content property. Here is the code for the converter that I used in my sample:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s