C# 如何在WinRT的代码隐藏中访问ResourceDictionary中的DataTemplate?
我在ResourceDictionary中定义了一个DataTemplate。我需要访问代码隐藏中的数据模板。(即)我需要将ResourceDictionary中定义的此DataTemplate设置为代码隐藏中的ContentControl.ContentTemplate属性。我怎样才能做到这一点。 下面是我的xaml代码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 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);