<p>How do I create a silverlight data template in code? I've seen plenty of examples for WPF, but nothing for Silverlight.</p>

<p>Edit: Here's the code I'm now using this for, based on the answer from Santiago below.</p>

<pre><code>public DataTemplate Create(Type type)
{
  return (DataTemplate)XamlReader.Load(
          @"&lt;DataTemplate
            xmlns=""http://schemas.microsoft.com/client/2007""&gt;
            &lt;" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/&gt;
            &lt;/DataTemplate&gt;"
   );
}
</code></pre>

<p>This works really nicely and allows me to change the binding on the fly. </p>

[{'answer_id': 62871, 'author': 'jarda', 'author_id': 6601, 'author_profile': 'https://Stackoverflow.com/users/6601', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.windows.datatemplate%28v=vs.95%29.aspx" rel="nofollow noreferrer">citation from MSDN</a>: </p>\n\n<blockquote>\n  <p>The XAML usage that defines the content for creating a data template is not exposed as a settable property. It is special behavior built into the XAML processing of a DataTemplate object element.</p>\n</blockquote>\n'}, {'answer_id': 72158, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': True, 'text': '<p>Although you cannot programatically create it, you can load it from a XAML string in code like this:</p>\n\n<pre><code>    public static DataTemplate Create(Type type)\n    {\n        return (DataTemplate) XamlReader.Load(\n            @"&lt;DataTemplate\n                xmlns=""http://schemas.microsoft.com/client/2007""&gt;\n                &lt;" + type.Name + @"/&gt;\n              &lt;/DataTemplate&gt;"\n          );\n    }\n</code></pre>\n\n<p>The snippet above creates a data template containing a single control, which may be a user control with the contents you need.</p>\n'}, {'answer_id': 356064, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>I had a few problems with this code, getting element not foung exceptions. Just for reference, it was that I needed my namesspace included in the DataTemplate...</p>\n\n<pre><code>private DataTemplate Create(Type type)\n        {\n            string xaml = @"&lt;DataTemplate \n                xmlns=""http://schemas.microsoft.com/client/2007""\n                xmlns:controls=""clr-namespace:" + type.Namespace + @";assembly=" + type.Namespace + @"""&gt;\n                &lt;controls:" + type.Name + @"/&gt;&lt;/DataTemplate&gt;";\n            return (DataTemplate)XamlReader.Load(xaml);\n        }\n</code></pre>\n'}, {'answer_id': 7101581, 'author': 'Davut Gürbüz', 'author_id': 413032, 'author_profile': 'https://Stackoverflow.com/users/413032', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, Silverligt 4 older than WPF\'s current versions. \nWhen you add a template as a resource i.e. as I did\nI added a userControl Template in Application.xaml MergedResources between ResourceDictionary.\nIn XAML if tag implemented IDictionary you could user x:Key attribute. Like that </p>\n\n<pre><code>   &lt;ResourceDictionary&gt;\n    &lt;DataTemplate x:Key="TextBoxEditTemplate"&gt;\n    &lt;Some user control x:Name="myOwnControl" /&gt;\n    &lt;/DataTemplate&gt;\n   &lt;/ResourceDictionary&gt;\n</code></pre>\n\n<p>Ok! You may reach your template by coding that, <strong>Application.Current.resources["TextBoxEditTemplate"]</strong>\non the other hand some methods for finding members of this template will not work. Beside this DataTemplate doesn\'t implement IDictionary so you cannot assign x:Key attribute for items in this dataTemplate. as myOwnControl in example.</p>\n\n<p>Without xaml current silverlight has some restrictions about creation fully dynamic code-behind DataTemplates.Even it works on WPF.</p>\n\n<p>Anyway the best solution by this point is creation of XAML script for datatemplate ,You may assing some values element in DataTemplate script. We created our own usercontrols has some properties with DependencyObjectProperty...</p>\n\n<p>At last if your object has no inherits ,i.e. not a MyControl:UserControl you may inherit <strong><em>MyObject:DependencyObject</em></strong> by this way you can reach your object by calling like Application.Current.Resources.FirstChilderen...</p>\n\n<p>FindName works only in WPF</p>\n'}]
['https://Stackoverflow.com/questions/59451', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5932/']