According to the SDK, the recommended way to programmatically create a template is to load XAML from a string or a memory stream using the
XamlReader.Load()
method. Believe me, that's the easiest way. It's very easy to build the necessary xaml string dynamically, and then load it.
To add the template to your Resources, you probably should create a
ResourceDictionary
with your template in it, use
XamlReader.Load
to load the xaml, and then merge the dictionary, like so.
this.Resources.MergedDictionaries.Add(myResourceDictionary);