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

  • SEARCH
  • Home
  • 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 7403325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:02:39+00:00 2026-05-29T05:02:39+00:00

I have a general question about data templates in WPF. Let’s say I have

  • 0

I have a general question about data templates in WPF. Let’s say I have an abstract class called “Question,” and various subclasses like “MathQuestion,” “GeographyQuestion,” etc. In some contexts, rendering the questions as a “Question” using the “Question” data template is good enough, but let’s say that I have a list of random Question objects of varying subclasses that I want to display in-turn. I want to display them to the user using their specific data templates rather than their generic Question data template, but since I don’t know that at design time, is there anyway to tell WPF, “hey, here’s a list of Quesitons, but use reflection to figure out their specific types and use THAT data template?”

What I’ve thought of so far: I thought that in addition to having my question collection, I could create another collection of the specific types using reflection and somehow bind that to “blah,” then I’d get the desired affect, but you can only bind to DependencyProperties in WPF, so I’m not sure what I’d bind to. I really don’t like this idea, and my gut tells me there’s a more elegant way to approach this problem.

I’m not looking for specific code here, just a general strategy to accomplish what I’m trying to do. Also, I’m using MVVM for the most part if that helps.

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-29T05:02:43+00:00Added an answer on May 29, 2026 at 5:02 am

    I’m thinking something like this should work right out of the box:

    <UserControl.Resources>
        <DataTemplate DataType="{x:Type vm:GenericQuestionViewModel}">
            <v:GenericQuestion/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type tvm:GeographyQuestionViewModel}">
            <tv:GeographyQuestion/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type tvm:BiologyQuestionViewModel}">
            <tv:BiologyQuestion/>
        </DataTemplate>
    </UserControl.Resources>
    
    <ContentControl Content="{Binding QuestionViewModel}">
    

    Edit:

    Yes, this definitely should work. Here’s a more complete example:

    Main View Model

    public class MainWindowViewModel : ViewModelBase
    {
        public ObservableCollection<QuestionViewModel> QuestionViewModels { get; set; }
    
        public MainWindowViewModel()
        {
            QuestionViewModels = new ObservableCollection<QuestionViewModel>
            {
                new GenericQuestionViewModel(),
                new GeographyQuestionViewModel(),
                new BiologyQuestionViewModel()
            };
        }
    }
    

    Question View Models

    public abstract class QuestionViewModel : ViewModelBase
    {
    }
    
    public class GenericQuestionViewModel : QuestionViewModel
    {
    }
    
    public class GeographyQuestionViewModel : QuestionViewModel
    {
    }
    
    public class BiologyQuestionViewModel : QuestionViewModel
    {
    }
    

    Question User Controls

    <UserControl x:Class="WpfApplication1.GenericQuestion" ...>
        <Grid>
            <TextBlock Text="Generic Question" />
        </Grid>
    </UserControl>
    
    <UserControl x:Class="WpfApplication1.GeographyQuestion" ...>
        <Grid>
            <TextBlock Text="Geography Question" />
        </Grid>
    </UserControl>
    
    <UserControl x:Class="WpfApplication1.BiologyQuestion" ...>
        <Grid>
            <TextBlock Text="Biology Question" />
        </Grid>
    </UserControl>
    

    Main Window

    <Window x:Class="WpfApplication1.MainWindow" ...
            Title="MainWindow"
            Height="900"
            Width="525">
        <Window.DataContext>
            <local:MainWindowViewModel />
        </Window.DataContext>
        <Window.Resources>
            <DataTemplate DataType="{x:Type local:GenericQuestionViewModel}">
                <local:GenericQuestion />
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:GeographyQuestionViewModel}">
                <local:GeographyQuestion />
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:BiologyQuestionViewModel}">
                <local:BiologyQuestion />
            </DataTemplate>
        </Window.Resources>
        <ItemsControl ItemsSource="{Binding QuestionViewModels}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Window>
    

    Update

    Kyle Tolle pointed out a nice simplification for setting ItemsControl.ItemTemplate. Here is the resulting code:

    <ItemsControl ItemsSource="{Binding QuestionViewModels}"
                  ItemTemplate="{Binding}" /> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A general architecture question in Sitecore 6... Let’s say we have a situation where
My question is about software architecture in general. Let's consider an example: We have
I have a general question about the way that database indexing works, particularly in
i have a general question about how sql server evaluates the joins.The query is
I have a general question for the group. I am about to start a
I have a General Class named MyGeneralClass: public MyGeneralClass<T> { public List<T> Data {get;
I have a general question about desktop applications. My friend and I were discussing
this is a General Question about MVC .. I wrote a PHP Class that
I have a general question about large in-memory objects and distributed computing. I have
I have a general question, and curiosity about LINQ and timeouts. I have a

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.