I have noticed some interest in my earlier post on how to add a WPF data template to a resource dictionary in code and so this post is a demonstration of the technique. The code is presented in F# but I have deliberately kept the code simple and fairly non-functional so that those of a C# or VB.NET background can follow the process easily.
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.
For those of you that do not have Microsoft Expression Blend (or an equivalent), using XAMLPad and Visual Studio can be okay. However, if you want to create custom controls that are similar to existing (standard or third party) controls then it can be tricky to find out how best to do it. This little utility uses the XamlWriter class to export a default template from a WPF control.