Creating a Silverlight DataTemplate in code

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

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

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

This works really nicely and allows me to change the binding on the fly.


Although you cannot programatically create it, you can load it from a XAML string in code like this:

    public static DataTemplate Create(Type type)
    {
        return (DataTemplate) XamlReader.Load(
            @"<DataTemplate
                xmlns=""http://schemas.microsoft.com/client/2007"">
                <" + type.Name + @"/>
              </DataTemplate>"
          );
    }

The snippet above creates a data template containing a single control, which may be a user control with the contents you need.


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...

private DataTemplate Create(Type type)
        {
            string xaml = @"<DataTemplate 
                xmlns=""http://schemas.microsoft.com/client/2007""
                xmlns:controls=""clr-namespace:" + type.Namespace + @";assembly=" + type.Namespace + @""">
                <controls:" + type.Name + @"/></DataTemplate>";
            return (DataTemplate)XamlReader.Load(xaml);
        }

Yes, Silverligt 4 older than WPF's current versions. When you add a template as a resource i.e. as I did I added a userControl Template in Application.xaml MergedResources between ResourceDictionary. In XAML if tag implemented IDictionary you could user x:Key attribute. Like that

   <ResourceDictionary>
    <DataTemplate x:Key="TextBoxEditTemplate">
    <Some user control x:Name="myOwnControl" />
    </DataTemplate>
   </ResourceDictionary>

Ok! You may reach your template by coding that, Application.Current.resources["TextBoxEditTemplate"] on 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.

Without xaml current silverlight has some restrictions about creation fully dynamic code-behind DataTemplates.Even it works on WPF.

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...

At last if your object has no inherits ,i.e. not a MyControl:UserControl you may inherit MyObject:DependencyObject by this way you can reach your object by calling like Application.Current.Resources.FirstChilderen...

FindName works only in WPF