Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609390
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:58:47+00:00 2026-05-22T00:58:47+00:00

Possible Duplicate: Datatemplate inheritance I have several data types that are not subclasses, nor

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:58:48+00:00Added an answer on May 22, 2026 at 12:58 am

    @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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Undefined Symbol ___gxx_personality_v0 on link I have a problem with the following
Possible Duplicate: How to run a database script file from Delphi? I have a
Possible Duplicate: Bind textbox to 'enter' key I have a search text box. I
Possible Duplicate: Image Button in BlackBerry Is there a way to have a picture
Possible Duplicate: PHP class instantiation. To use or not to use the parenthesis? I'm
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.