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 469227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:45:41+00:00 2026-05-12T23:45:41+00:00

I have two HeaderedContentControl s like those below that each have their content property

  • 0

I have two HeaderedContentControls like those below that each have their content property bound to one of two view model properties of the same base type (one control is on the left side of the window and one on the right, thus the view model property names).

However, either view model property can be one of four different derived types. So the left could be an Airplane and the right can be a Car. Then later, the left could be a Boat and right could be an Airplane. I would like the Style property of the header controls to be dynamic based on the derived type. What’s the best way to do this declaratively?

<Window...>
    <StackPanel 
        Grid.Row="2"
        Orientation="Horizontal" VerticalAlignment="Top">
        <Border 
            Height="380" 
            Width="330"
            Margin="0,0,4,0"
            Style="{StaticResource MainBorderStyle}">
            <HeaderedContentControl
                Content="{Binding Path=LeftChild}"
                Header="{Binding LeftChild.DisplayName}"
                Style="{StaticResource StandardHeaderStyle}"
            />
        </Border>

        <Border 
            Height="380" 
            Width="330"
            Style="{StaticResource MainBorderStyle}">
            <HeaderedContentControl
                Content="{Binding Path=RightChild}"
                Header="{Binding RightChild.DisplayName}"
                Style="{StaticResource StandardHeaderStyle}"
            />  
        </Border>
    </StackPanel>
</Window>


<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:myViewModelNamespace;assembly=myViewModelAssembly"
    xmlns:vw="clr-namespace:myViewNamespace" >

    <!--***** Item Data Templates ****-->
    <DataTemplate DataType="{x:Type vm:CarViewModel}">
        <vw:CarView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:BoatViewModel}">
        <vw:BoatView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:AirplaneViewModel}">
        <vw:AirplaneView />
    </DataTemplate>

    <!--***** 
        Other stuff including the StandardHeaderStyle and the MainBorderStyle
    ****-->

</ResourceDictionary>
  • 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-12T23:45:41+00:00Added an answer on May 12, 2026 at 11:45 pm

    My answer is an elaboration on Archimed’s. Don’t hesitate to ask further!

    <Window x:Class="Datatemplate_selector.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    xmlns:local="clr-namespace:Datatemplate_selector">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:CarDetail}">
         <Border BorderBrush="Yellow" BorderThickness="2">
            <HeaderedContentControl Margin="4" Foreground="Red">
                <HeaderedContentControl.Header>
                    <Border BorderBrush="Aquamarine" BorderThickness="3">
                        <TextBlock Text="{Binding Name}"/>
                    </Border>
                 </HeaderedContentControl.Header>
                <HeaderedContentControl.Content>
                    <Border BorderBrush="CadetBlue" BorderThickness="1">
                        <TextBlock TextWrapping="Wrap" Text="{Binding Description}"/>
                    </Border>
                </HeaderedContentControl.Content>
            </HeaderedContentControl>
           </Border>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:HouseDetail}">
            <HeaderedContentControl Margin="4" Foreground="Yellow" FontSize="20"
                        Header="{Binding Name}">
                <HeaderedContentControl.Content>
                    <TextBlock Foreground="BurlyWood"  TextWrapping="Wrap"
                               Text="{Binding Description}"/>
                </HeaderedContentControl.Content>
            </HeaderedContentControl>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ItemDetail}">
            <HeaderedContentControl Margin="4" Foreground="Green" FontStyle="Italic" 
                        Content="{Binding Description}"
                        Header="{Binding Name}">
            </HeaderedContentControl>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding ItemDetails}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="2"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
    

    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace Datatemplate_selector
    {
       public partial class Window1 : Window
        {
            public ObservableCollection<ItemDetail> ItemDetails { get; set; }
    
            public Window1()
            {
                ItemDetails = new ObservableCollection<ItemDetail>
                                  {
                                      new CarDetail{Name="Trabant"},
                                      new HouseDetail{Name="Taj Mahal"}
                                  };
                DataContext = this;
                InitializeComponent();
            }
        }
    
        public class ItemDetail:DependencyObject
        {
            public string Name
            {
                get { return (string)GetValue(NameProperty); }
                set { SetValue(NameProperty, value); }
            }
    
            public static readonly DependencyProperty NameProperty =
                DependencyProperty.Register("Name",
                typeof(string),
                typeof(ItemDetail),
                new UIPropertyMetadata(string.Empty));
    
            public virtual string Description
            {
                 get { return Name + " has a lot of details"; }
            }
        }
    
        public class CarDetail:ItemDetail
        {
            public override string Description
            {
                get { return string.Format("The car {0} has two doors and a max speed of 90 kms/hr", Name); }
            }
        }
    
        public class HouseDetail:ItemDetail
        {
            public override string Description
            {
                get { return string.Format("The house {0} has two doors and a backyard", Name); }
            }
        }
    }
    

    PS: I thought that this use of inheritance in a generic collection was not supported in .Net 3. I am pleasurably surprised that this code works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two applications written in Java that communicate with each other using XML
i have two richtextboxes one below the other in my application.when the user start
I have two classes, and want to include a static instance of one class
I have two threads, one needs to poll a bunch of separate static resources
I have two spreadsheets... when one gets modified in a certain way I want
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
Have two folders with approx. 150 java property files. In a shell script, how
I have a script that appends some rows to a table. One of the
I have two textboxes, the first of them I've bound to a tabControl's Item's
Have two bean definitions: file a.xml <bean id=A class=com.A> <property name=bClass ref=B/> </bean> file

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.