Possible Duplicate:
Datatemplate inheritance
I have several data types that are not subclasses, nor do they share an interface, but they do have common properties that I want to display in a XAML DataTemplate. That said, I know this is possible...
<!-- Basic Style Inheritance -->
<Style x:Key="FooStyle" TargetType="Foo" />
<Style x:Key="EnhancedFooStyle" TargetType="Foo" BasedOn="{StaticResource FooStyle}" />
<!-- Inheritance By Type -->
<Style x:Key="BaseItemStyle">
<Setter Property="Control.Background" Value="Yellow" />
</Style>
<!-- These three data types share the same 'BaseItemStyle' -->
<Style TargetType="ListBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="TreeViewItem" BasedOn="{StaticResource BaseItemStyle}" />
But can we do something similar like this for data templates which don't have the BasedOn
property?
<DataTemplate x:Key="CommonTemplate">
<!-- Common Stuff Goes Here -->
</DataTemplate>
<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}" />
<DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" />
<DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" />
I know BasedOn
isn't what we want here because it's not 'based on' but rather 'is' in this scenario, but not sure how to do that purely in XAML. As I'm writing this, I have an idea, but I feel using UserControl
is cheating...
<UserControl x:Key="CommonTemplate" x:Shared="False">
<!-- Common Stuff Goes Here -->
</UserControl>
<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}">
<StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>
<DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" />
<StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>
<DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" />
<StaticResource ResourceKey="CommonTemplate" />
</DataTemplate>
Thanks!