1

I have defined a DataTemplate inside a ResourceDictionary. I need to access the DataTemplate in the code behind. (i.e) I need to set this DataTemplate defined inside a ResourceDictionary to a ContentControl.ContentTemplate property in the code behind. How can i achieve this. Below is the my xaml code.

<ResourceDictionary>
...
<DataTemplate x:Key="template">
        <Border BorderThickness="2" 
                BorderBrush="Black">
            <TextBlock Text="{Binding Path=value}" Padding="2" />
        </Border>
    </DataTemplate>
...
</ResourceDictionary>

This is the code that I tried:

ResourceDictionary rd = new ResourceDictionary() { Source = new Uri("ms-appx:///sample.xaml", UriKind.Absolute) };
object tem;
rd.TryGetValue("template", out tem);

Can anyone point me how to access it?

Harikrishnan
  • 1,474
  • 2
  • 11
  • 25
  • What is wrong with the code you've tried? You should cast `tem` into `DataTemplate`. – Erti-Chris Eelmaa Jan 02 '15 at 14:39
  • 1
    I just made a sample project to try out your approach and it works perfectly. You need to cast `tem` as Chris already pointed out, but that's it. What errors do you get? Maybe your `Uri` for the `ResourceDictionary` is wrong. – Damir Arh Jan 03 '15 at 07:20

0 Answers0