Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WinRT的代码隐藏中访问ResourceDictionary中的DataTemplate?_C#_Winrt Xaml_Datatemplate_Controltemplate_Winrt Component - Fatal编程技术网

C# 如何在WinRT的代码隐藏中访问ResourceDictionary中的DataTemplate?

C# 如何在WinRT的代码隐藏中访问ResourceDictionary中的DataTemplate?,c#,winrt-xaml,datatemplate,controltemplate,winrt-component,C#,Winrt Xaml,Datatemplate,Controltemplate,Winrt Component,我在ResourceDictionary中定义了一个DataTemplate。我需要访问代码隐藏中的数据模板。(即)我需要将ResourceDictionary中定义的此DataTemplate设置为代码隐藏中的ContentControl.ContentTemplate属性。我怎样才能做到这一点。 下面是我的xaml代码 <ResourceDictionary> ... <DataTemplate x:Key="template"> <Border

我在ResourceDictionary中定义了一个DataTemplate。我需要访问代码隐藏中的数据模板。(即)我需要将ResourceDictionary中定义的此DataTemplate设置为代码隐藏中的ContentControl.ContentTemplate属性。我怎样才能做到这一点。 下面是我的xaml代码

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

有人能告诉我如何访问它吗?

您尝试的代码有什么问题吗?您应该将
tem
转换为
DataTemplate
。我刚刚制作了一个示例项目来尝试您的方法,它非常有效。正如Chris已经指出的,你需要施展
tem
,但仅此而已。你会犯什么错误?可能您的
ResourceDictionary
Uri
是错误的。
ResourceDictionary rd = new ResourceDictionary() { Source = new Uri("ms-appx:///sample.xaml", UriKind.Absolute) };
object tem;
rd.TryGetValue("template", out tem);