2017-01-18 37 views
0

我在某些其他問題上看到了沿着這些線條做了{DynamicResource xyz}的一些事情,但它似乎不適用於UWP。這是我的XAML:如何在UWP XAML資源中自引用DataTemplate?

<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> 

我想自我參照的DataTemplateItemsControlItemTemplate財產。我將如何去取代它?

+0

我不確定我是否有答案,但您可以嘗試使用ItemTemplate =「{StaticResource commentTemplate}」或{Binding RelativeSource Mode = Self}替換ItemTemplate =「{RelativeSource Mode = Self}」。讓我知道如果它的工作,否則我會嘗試一下。另外,如果你可以提供一個樣本數據JSON或其他東西,以便我可以測試代碼 –

+0

我已經嘗試過使用'{StaticResource commentTemplate}'引用它,但它似乎不支持自引用。它總是拋出一個錯誤「無法找到資源...」。我檢查了你的另一個解決方案,它不再拋出異常。它似乎在起作用,至少是部分的。當我添加任何嵌套註釋時,它會嘗試轉換相對綁定,出於某種原因:' – Reynevan

+0

'錯誤:轉換器未能將類型'Windows.UI.Xaml.Controls.ItemsControl'的值轉換爲鍵入'DataTemplate'; BindingExpression:Path =''DataItem ='Windows.UI.Xaml.Controls.ItemsControl';目標元素是'Windows.UI.Xaml.Controls.ItemsControl'(Name ='null');目標屬性是'ItemTemplate'(類型'DataTemplate')。 ' – Reynevan

回答

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); 
    } 
} 

我的MainPage:

<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); 
     } 
    } 
}