Let s say you have your DataTemplate
in a ResourceDictionary "DataTemplates.xaml". Note that you need to use Binding
instead of x:Bind
.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DataTemplateExample">
<DataTemplate x:Key="SomeViewTemplate">
<StackPanel>
<TextBlock Text="{Binding val1}"/>
<RatingControl MaxRating="{Binding val2}" />
</StackPanel>
</DataTemplate>
</ResourceDictionary>
Add it to App.xaml,
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
<ResourceDictionary Source="DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
</ResourceDictionary>
</Application.Resources>
Then you should be able to use it.
<ListView
ItemTemplate="{StaticResource SomeViewTemplate}"
ItemsSource="{x:Bind Items, Mode=OneWay}" />