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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:39:40+00:00 2026-05-21T10:39:40+00:00

I have created the following model (the code is simplified to illustrate the situation):

  • 0

I have created the following model (the code is simplified to illustrate the situation):

public abstract class Account
{
    public string Name { get; set; }
}

public class Person : Account
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Company : Account
{
    public string Owner { get; set; }
}

Next I have created a view model:

public class ViewModel
{
    public Account Model { ... }
    public string Name { ... }
    public string FirstName { ... }
    public string LastName { ... }
    public string Owner { ... }   
    ...
}

And finally, the view:

<UserControl>
  <UserControl.Resources>

    <!-- Person data template -->
    <DataTemplate x:Key="personTemplate" DataType="{x:Type model:Person}">
      <Grid DataContext="{Binding ElementName=rootLayout, Path=DataContext}">
        <TextBlock Text="{Binding Path=Name}" />
        <TextBlock Text="{Binding Path=FirstName}" />
        <TextBlock Text="{Binding Path=LastName}" />
      </Grid>
    </DataTemplate>

    <!-- Company data template -->
    <DataTemplate x:Key="companyTemplate" DataType="{x:Type model:Company}">
      <Grid DataContext="{Binding ElementName=rootLayout, Path=DataContext}">
        <TextBlock Text="{Binding Path=Name}" />
        <TextBlock Text="{Binding Path=Owner}" />
      </Grid>
    </DataTemplate>

    <!-- Data template selector for different account types -->
    <local:AccountTemplateSelector x:Key="templateSelector" 
        PersonTemplate="{StaticResource personTemplate}" 
        CompanyTemplate="{StaticResource companyTemplate}" />

  </UserControl.Resources>

  <StackPanel Name="rootLayout" DataContext="{Binding Path=viewModel}">
    <ContentControl Content="{Binding Path=Model}" 
        ContentTemplateSelector="{StaticResource templateSelector}"/>
    <Button Content="Save" />
    <Button Content="Close" />
  </StackPanel>

</UserControl>

So, when the model that is loaded is of type Person the personTemplate is shown; vice versa, when the model is Company the companyTemplate is shown.

My questions are:

  1. Does this approach make sense at all? Would it be smarter to delete the Model
    property in the ViewModel class and to introduce an enum or just a simple bool
    which would show person if true, or company if `false?
  2. While defining the data templates, I specified DataTypes to Person and Company
    types (it was natural to me to do it this way). Do I need it at all because in the very
    next line I am setting a new data context to be the one from the UserControl?
  3. Should the DataTypes of the data templates be different view models, something like
    PersonViewModel and CompanyViewModel? Does it make sense to create them?
  4. How can I, and can I at all, make data template inherit the data context from the
    ContentControl automatically?

I know that all this is a matter of a personal choice in the end, but since I am learning MVVM (I am using MVVM Light), I am wondering which approach would be the most recommendable one? I still do not fully understand when should the classes from models be used as data types for data templates and when should view models be used for that purpose. Should the assembly that represents the model even be referenced in the view assembly (assuming that view, model and view model all reside in separate assemblies)?

Thanks for all the clarifications!

UPDATE:

This update should explain the problem of having classes of the model as DataTypes in the data templates when the property of the model class is not directly binded to just one control in the view.

There is an enum and a new property in the Person, so now it looks like this:

public class Person : Account
{
    public enum GenderType { Female, Male, NotSpecified }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public GenderType Gender {get; set; }
}

And in the view, the data template of the person is changed as well of course:

<!-- Person data template -->
<DataTemplate x:Key="personTemplate" DataType="{x:Type model:Person}">
  <Grid DataContext="{Binding ElementName=rootLayout, Path=DataContext}">
    <TextBlock Text="{Binding Path=Name}" />
    <TextBlock Text="{Binding Path=FirstName}" />
    <TextBlock Text="{Binding Path=LastName}" />
    <RadioButton Name="Female" />
    <RadioButton Name="Male" />
    <RadioButton Name="NotSpecified" />
  </Grid>
</DataTemplate>

If the Content of the ContentControl is set to Model property of the ViewModel, how would I resolve the gender/radio buttons situation; because, now they do not match in the way one control/one property?

  • 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-21T10:39:41+00:00Added an answer on May 21, 2026 at 10:39 am

    I would change it to this:

    <UserControl>
      <UserControl.Resources>
        <!-- Person data template -->
        <DataTemplate DataType="{x:Type model:Person}">
          <Grid>
            <TextBlock Text="{Binding Path=Name}" />
            <TextBlock Text="{Binding Path=FirstName}" />
            <TextBlock Text="{Binding Path=LastName}" />
            <RadioButton Name="Female"       IsChecked="{Binding Gender , Converter={StaticResource enumBooleanConverter}, ConverterParameter=Female}" />
            <RadioButton Name="Male"         IsChecked="{Binding Gender , Converter={StaticResource enumBooleanConverter}, ConverterParameter=Male}" />
            <RadioButton Name="NotSpecified" IsChecked="{Binding Gender , Converter={StaticResource enumBooleanConverter}, ConverterParameter=NotSpecified }" />
          </Grid>
        </DataTemplate>
    
        <!-- Company data template -->
        <DataTemplate DataType="{x:Type model:Company}">
          <Grid>
            <TextBlock Text="{Binding Path=Name}" />
            <TextBlock Text="{Binding Path=Owner}" />
          </Grid>
        </DataTemplate>
      </UserControl.Resources>
    
      <StackPanel DataContext="{Binding viewModel}">
        <ContentControl Content="{Binding Model}" />
        <Button Content="Save" />
        <Button Content="Save" />
        <Button Content="Close" />
      </StackPanel>
    
    </UserControl>
    

    like this you define implicit styles for your classes and you don’t have to use a templateselector. Also then you don’t need all your string properties in the ViewModel class:

    public class ViewModel
    {
        public Account Model { ... } 
        ...
    }
    

    Disclaimer, the binding in the RadioButtons uses a Converter from here.

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

Sidebar

Related Questions

I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have the following model: create_table mouldings, :force => true do |t| t.string suppliers_code
I have the following model: create_table material_costs, :force => true do |t| t.string material
I have created a model POCO class called Recipe ; a corresponding RecipeRepository persists
I have the following code <ul id =menu> <%foreach (var pckg in Model) {
I have following model: class Customer < ActiveRecord::Base has_one :cart end class Cart <
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have the following code to create a UIPickerView: pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,

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.