Programmatic addition of a WPF DataTemplate to a ResourceDictionary

If you need (or want) to add a DataTemplate (or related class) object to a WPF resource dictionary programmatically then you need to understand how XAML data templates are added to the resource dictionary.

Resource dictionaries identify resources by a key. In general, you set this key using the XAML markup element “x:Key” but this is optional in the case of DataTemplate instances. When a key is not provided, the XAML reader accesses the DataTemplateKey property which uses the DataType property on the data template to construct an instance of DataTemplateKey passing the Type class instance for the type used in the DataType property. Why does the XAML reader do this?

It happens that the DataTemplate class has an attribute on it: System.Windows.Markup.DictionaryKeyPropertyAttribute. You will find this online in the MSDN documentation and possibly in recent Windows SDKs, although it is not documented in the one I downloaded fairly recently (but the most recent version would not install for some reason!). Here is the link:

If you want to replace the default data template for a class, rather than creating a new keyed resource, and you want to do it programmatically, then here is the code (in C# for a change from recent posts):

void addDataTemplate(
      ResourceDictionary targetResources, 
      DataTemplate template, 
      Type dataType)
      new DataTemplateKey(dataType), 

MSDN recommends that content templates are loaded using the XamlReader class. This can meet the needs of programmatic creation, but it would still be nice to have a direct programmatic alternative (rather than marshalling through XAML). I may look into this further in a future post.

Update:I recommend against creating templates entirely programmatically. Instead, construct XAML programmatically and then use XamlReader.Load. There are several blog entries outlining how to create these templates using the FrameworkElementFactory class. However, as well as being deprecated, this class does not appear to be in some (possibly all) Silverlight engines and it also does not easily handle the distinction between the logical and visual trees. There is a real risk that the code will be both unportable and incorrect, even if it appears to work. So, concentrate your efforts on dynamically generating reusable XAML that can be used safely.

About these ads

One thought on “Programmatic addition of a WPF DataTemplate to a ResourceDictionary

  1. Pingback: Worked Example: Programmatic Addition of a WPF Data Template to a Resource Dictionary « Steve Horsfield

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