如何在UWP XAML资源中自引用DataTemplate?

时间:2017-01-18 09:52:21

标签: xaml uwp datatemplate

我已经在{DynamicResource xyz}上看到了其他一些问题,但是它似乎与UWP无关。这是我的XAML:

<DataTemplate x:Key="commentTemplate">
    <StackPanel>
        <Grid Margin="4" Background="#40606060" MinHeight="64">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <StackPanel>
                <TextBlock Margin="4" FontSize="14" TextWrapping="WrapWholeWords" Text="{Binding Path=Message, Mode=OneWay}" />
                <Image MaxHeight="96" Source="{Binding Path=Image}" HorizontalAlignment="Left" Margin="4" />
            </StackPanel>
            <StackPanel Background="#18808080" Orientation="Horizontal" Grid.Row="1">
                <FontIcon Margin="2,0" Glyph="&#xE19D;" />
                <TextBlock Margin="2,0" Text="{Binding Path=Score, Mode=OneWay}" />
                <Button Content="Reply" Margin="2,0" />
            </StackPanel>
        </Grid>
        <ItemsControl ItemTemplate="{RelativeSource Mode=Self}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="48" ItemsSource="{Binding Path=Comments}" />
    </StackPanel>
</DataTemplate>

我想自我引用DataTemplate的{​​{1}}属性中的ItemsControl。我该如何更换呢?

1 个答案:

答案 0 :(得分:1)

这可能适合您。我在POS应用程序中使用它 - 虽然这是我第一次尝试嵌套它。 xaml设计器因为在声明之前的选择器StaticResource引用而抱怨,但它在运行时工作。

基本上,我使用它来根据绑定到项目的类选择不同的数据模板。在这种情况下,只有一种类型,所以我们只是使用类类型来决定使用哪种模板。

为MyModel:

public class MyModel
{
    public int Id { get; set; }
    public string Desc { get; set; }
    public List<MyModel> Children { get; set; }
}

MyTemplateSelector:

public class MyTemplateSelector : DataTemplateSelector
{
    public DataTemplate MyModelTemplate { get; set; }
    protected override DataTemplate SelectTemplateCore(object item)
    {
        if (item is MyModel)
        {
            return MyModelTemplate;
        }
        else
        {
            return null;
        }
    }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        return SelectTemplateCore(item);
    }
}

我的主页:

<Page
    x:Class="App7.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App7"
    xmlns:models="using:App7.Models"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>

        <DataTemplate x:Key="myModelTemplate" x:DataType="models:MyModel">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock Text="{x:Bind Path=Desc, Mode=OneWay}" />
                <ListView Grid.Row="1" ItemTemplateSelector="{StaticResource selector}" ItemsSource="{x:Bind Path=Children, Mode=OneWay}" />
            </Grid>
        </DataTemplate>
        <local:MyTemplateSelector x:Key="selector" MyModelTemplate="{StaticResource myModelTemplate}" />
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="lstView" ItemTemplateSelector="{StaticResource selector}" >

        </ListView>
    </Grid>
</Page>

我的代码背后:

public sealed partial class MainPage : Page
{
    List<MyModel> lst { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        BuildList();
        lstView.ItemsSource = lst;
    }

    private void BuildList()
    {
        lst = new List<MyModel>();
        for (int i = 0; i < 10; i++)
        {
            MyModel mod = new MyModel();
            mod.Id = i;
            mod.Desc = "Desc" + i.ToString();
            mod.Children = new List<MyModel>();

            for (int j = 100; j < 102; j++)
            {
                MyModel mod2 = new MyModel();
                mod2.Id = j;
                mod2.Desc = "Desc" + j.ToString();
                mod2.Children = new List<MyModel>();
                for (int k = 1000; k < 1002; k++)
                {
                    MyModel mod3 = new MyModel();
                    mod3.Id = k;
                    mod3.Desc = "Desc" + k.ToString();
                    mod3.Children = new List<MyModel>();
                    mod2.Children.Add(mod3);
                }
                mod.Children.Add(mod2);
            }
            lst.Add(mod);
        }
    }
}