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="" />
<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
在ItemsControl
的ItemTemplate
財產。我將如何去取代它?
我不確定我是否有答案,但您可以嘗試使用ItemTemplate =「{StaticResource commentTemplate}」或{Binding RelativeSource Mode = Self}替換ItemTemplate =「{RelativeSource Mode = Self}」。讓我知道如果它的工作,否則我會嘗試一下。另外,如果你可以提供一個樣本數據JSON或其他東西,以便我可以測試代碼 –
我已經嘗試過使用'{StaticResource commentTemplate}'引用它,但它似乎不支持自引用。它總是拋出一個錯誤「無法找到資源...」。我檢查了你的另一個解決方案,它不再拋出異常。它似乎在起作用,至少是部分的。當我添加任何嵌套註釋時,它會嘗試轉換相對綁定,出於某種原因:' – Reynevan
'錯誤:轉換器未能將類型'Windows.UI.Xaml.Controls.ItemsControl'的值轉換爲鍵入'DataTemplate'; BindingExpression:Path =''DataItem ='Windows.UI.Xaml.Controls.ItemsControl';目標元素是'Windows.UI.Xaml.Controls.ItemsControl'(Name ='null');目標屬性是'ItemTemplate'(類型'DataTemplate')。 ' – Reynevan