Is there a way for two DataTemplates to share some elements?
Something开发者_如何学C like a template or base DataTemplate which both DataTemplates extend or inherit so I don't have to duplicate XAML.
What about UserControls? Create a base UserControl and then extend the second one?
<DataTemplate>
<local:MyBase />
</DataTemplate>
And extend it like this?
<DataTemplate>
<local:MyBase />
<local:SomeOtherStuff />
</DateTemplate>
You can nest DataTemplates. Here an is example
<DataTemplate x:Key="InnerTemplate">
<TextBlock Text="{Binding}" Foreground="Purple" />
</DataTemplate>
<DataTemplate x:Key="OuterTemplate">
<StackPanel>
<TextBlock Text="Header" Foreground="Red" />
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource InnerTemplate}" />
</StackPanel>
</DataTemplate>
In this case I just have a List bound to a listbox, and its itemtemplate set to the OuterTemplate template.
<ListBox x:Name="_lbTest" Grid.Row="1" ItemTemplate="{StaticResource OuterTemplate}" ></ListBox>
精彩评论