In WPF can you base one DataTemplate on another like you can with a Style? [duplicate]
Asked Answered
Z

1

10

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!

Zilvia answered 6/5, 2011 at 23:49 Comment(0)
Z
6

@Foovanadil, actually I think I came up with something better. My new approach not only avoids the extra binding (the one on the content presenter), but also removes the need to have the template be applied at all by that presenter as you're explicitly setting its contents. Both of these things should speed up your UI, especially in larger, more complex interfaces.

<Border x:Shared="False" x:Key="Foo" BorderBrush="Red" BorderThickness="1" CornerRadius="4">
    <TextBlock Text="{Binding SomeProp}" />
</Border>

<DataTemplate x:Key="TemplateA">
    <ContentPresenter Content="{StaticResource Foo}" />
</DataTemplate>

<DataTemplate x:Key="TemplateB">
    <ContentPresenter Content="{StaticResource Foo}" />
</DataTemplate>

Important: Make sure to use the x:Shared attribute on your shared content or this will not work.

M

Zilvia answered 7/5, 2011 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.