tags:

views:

18

answers:

2

Something like a template or base DataTemplate which both DataTemplates extend or inherit so I don't have to duplicate XAML.

A: 

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>
rudigrobler
A: 

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>
mdm20
I actually like the idea of nesting data templates better than pulling out chunks of my data template into a user control. Unfortunately, the chunk I want to share is the outer template so this approach is not possible in my case.
Chry Cheng