28

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.

1
  • I couldn't get this to work. Problems explained here Commented Aug 24, 2011 at 5:43

4 Answers 4

39

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer - no idea why I didn't think of it myself!
11

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);
        }

2 Comments

You don't need the statement "DataTemplate dt = new DataTemplate();" because it is not used.
Assembly name != Namespace name though.
4

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

Comments

2

citation from MSDN:

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.

2 Comments

Where did you find that? Can you provide a URL?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.