Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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# 动态绑定到数据模板_C#_Xaml_Data Binding_Windows 8_Windows Runtime - Fatal编程技术网

C# 动态绑定到数据模板

C# 动态绑定到数据模板,c#,xaml,data-binding,windows-8,windows-runtime,C#,Xaml,Data Binding,Windows 8,Windows Runtime,我有一个ListBox和多个DataTemplates,在单独的文件中 <ListBox ItemTemplate="{StaticResource ItemTemplate1}"/> 我想根据列表中对象的类型更改列表框的ItemTemplate 是否有办法访问代码隐藏中单独的数据模板s,以便我可以绑定到我的页面的属性?尝试此解决方案可以实现您想要实现的目标: 在WP7中按类型动态应用数据模板 它基于WP7,但也应该适用于您。WPF中内置了对您的需求的支持。开始阅读,根据特定条件

我有一个
ListBox
和多个
DataTemplate
s,在单独的文件中

<ListBox ItemTemplate="{StaticResource ItemTemplate1}"/>
我想根据列表中对象的类型更改列表框的
ItemTemplate


是否有办法访问代码隐藏中单独的
数据模板
s,以便我可以绑定到我的
页面
的属性?

尝试此解决方案可以实现您想要实现的目标:

在WP7中按类型动态应用数据模板


它基于WP7,但也应该适用于您。

WPF中内置了对您的需求的支持。开始阅读,根据特定条件在运行时选择模板。

不使用
TemplateSelector
的方法是指定数据类型属性,而不指定x:Key

<DataTemplate DataType="{x:Type Type1}">...</DataTemplate>
<DataTemplate DataType="{x:Type Type2}">...</DataTemplate>
然后您可以获得模板:

var dataTemplate = (DataTemplate) dict["ItemTemplate1"];

仅供参考。WinRT DataTemplate类不支持数据类型。
var dict = new ResourceDictionary 
{Source = new Uri("/ProjectNamespace;component/Styles.xaml",  UriKind.Relative)};
var dataTemplate = (DataTemplate) dict["ItemTemplate1"];