Reputation: 47
I have the datatemplate:
<DataTemplate x:Key="ItemWithCanvas"
x:DataType="itemVM">
<Canvas>
<Grid x:Name="common">
...
</Grid>
</Canvas>
</DataTemplate>
Now, I want a similar datatemplate but no Canvas, instead of duplicating the code:
<DataTemplate x:Key="ItemWithNoCanvas"
x:DataType="itemVM">
<Grid x:Name="common">
...
</Grid>
</DataTemplate>
What should I do to avoid duplicating as above?
Upvotes: 0
Views: 53
Reputation: 13771
You can do something like this.
<Grid>
<Grid.Resources>
<DataTemplate x:Key="CommonContent" x:DataType="local:ItemVM">
<Grid ColumnDefinitions="*,*">
<TextBlock Grid.Column="0" Text="{x:Bind FirstName}" />
<TextBlock Grid.Column="1" Text="{x:Bind LastName}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="ItemWithCanvas" x:DataType="local:ItemVM">
<Canvas>
<ContentControl x:Name="Common" ContentTemplate="{StaticResource CommonContent}" />
</Canvas>
</DataTemplate>
<DataTemplate x:Key="ItemWithoutCanvas" x:DataType="local:ItemVM">
<ContentControl x:Name="Common" ContentTemplate="{StaticResource CommonContent}" />
</DataTemplate>
</Grid.Resources>
<StackPanel>
<ItemsRepeater ItemTemplate="{StaticResource ItemWithoutCanvas}" ItemsSource="{x:Bind Items}" />
<ItemsRepeater ItemTemplate="{StaticResource ItemWithCanvas}" ItemsSource="{x:Bind Items}" />
</StackPanel>
</Grid>
Upvotes: 1