开发者

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

开发者 https://www.devze.com 2023-03-03 18:51 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Datatemplate inheritance
This question already has answers here: Closed 11 years ago.

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 开发者_如何学JAVAsame '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!


@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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号