我想在App.XAML中定义Datatemplate,然后为我需要使用此itemtemplate的任何页面共享它.我不知道怎么做
Dam*_*Arh 14
这取决于您想要使用的绑定类型.
如果您使用标准XAML绑定,则所有内容都与WPF中的相同:
在Application.Resources以下位置定义模板:
<Application.Resources>
<DataTemplate x:Key="Template1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Prop1}" />
<TextBox Text="{Binding Prop2}" />
</StackPanel>
</DataTemplate>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)在页面中引用模板:
<ListView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource Template1}" />
Run Code Online (Sandbox Code Playgroud)如果您正在使用编译{x:bind}绑定,则需要在单独的资源字典中定义模板,其后面的代码将生成代码:
为资源字典创建一个新的分部类:
public partial class DataTemplates
{
public DataTemplates()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)使用数据模板基于此分部类创建资源字典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyNamespace"
x:Class="MyNamespace.DataTemplates">
<DataTemplate x:Key="Template2" x:DataType="local:MyClass">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Prop1}" />
<TextBox Text="{x:Bind Prop2}" />
</StackPanel>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)将资源字典合并到Application.Resources:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:DataTemplates/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)最后使用页面中的模板:
<ListView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource Template2}" />
Run Code Online (Sandbox Code Playgroud)您可以查看Igor的博客文章了解更多详情.自帖子发布后的预览以来,没有任何重大变化.