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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:29:04+00:00 2026-06-12T15:29:04+00:00

I have a view model that currently looks like below. The TCm type is

  • 0

I have a view model that currently looks like below. The TCm type is a domain model type, ie TelephoneNumber. And there are subclassed PcmDetailVms (ie, TelephoneNumberPcmDetailVm). I want to use these properties for a DataGrid that deals the specific TCm (ie, TelephoneNumber).

While it isn’t horrible at all to subclass it and do the casting I am wondering if there is any built-in xaml features that would do the casting. Or if it would be more idiomatic to use a converter?

Cheers,
Berryl

view model

public class PcmShellVm<TCm> : ... where TCm : ContactMechanism
{
    public ObservableCollection<PcmDetailVm> DetailVms { get; protected set; }

    public PcmDetailVm SelectedVm {
        get { return _collectionView.CurrentItem as PcmDetailVm; } 
        set { _collectionView.MoveCurrentTo(value); }
    }
}

subclassed vm

public class TelecomNumberPcmShellVm : PcmShellVm<TelecomNumber>
{
    ...

    public IEnumerable<TelecomPcmDetailVm> CastedDetailVms { get { return DetailVms.Cast<TelecomPcmDetailVm>(); } }

    public TelecomPcmDetailVm CastedSelectedVm 
    {
        get { return (TelecomPcmDetailVm) SelectedVm; } 
        set { SelectedVm = value; }
    }
}

EDIT

So Andrei had the right answer to the question I did not ask very clearly above. I had incorrectly assumed I needed to cast my items somewhere in xaml, as I would need to do in code.

The binding engine must be using reflection to discover properties by name however, making the cast unnecessary.

  • 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-06-12T15:29:05+00:00Added an answer on June 12, 2026 at 3:29 pm

    I am not sure I understood the question correctly but I think specific DataTemplates for each type would be an idea.

    View :

    <Window x:Class="WpfApplication18.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:WpfApplication18="clr-namespace:WpfApplication18"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <DataTemplate DataType="{x:Type WpfApplication18:PersonsVM}">
                    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding FirstName}" Header="First name"/>
                            <DataGridTextColumn Binding="{Binding LastName}" Header="Last name"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
                <DataTemplate DataType="{x:Type WpfApplication18:SpecificPersonsVM}">
                    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding LastName}" Header="Last name"/>
                            <DataGridTextColumn Binding="{Binding FirstName}" Header="First name"/>
                            <DataGridTextColumn Binding="{Binding Age}" Header="Age"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </Grid.Resources>
    
            <ContentControl Content="{Binding}"/>
        </Grid>
    </Window>
    

    ViewModels :

    using System.Collections.ObjectModel;
    
    namespace WpfApplication18
    {
        public class PersonVM
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    
        public class SpecificPersonVM : PersonVM
        {
            public byte Age { get; set; }
        }
    
        public class PersonsVM
        {
            public ObservableCollection<PersonVM> Items { get; private set; }
    
            public PersonsVM()
            {
                Items = new ObservableCollection<PersonVM>();
            }
        }
    
        public class SpecificPersonsVM
        {
            public ObservableCollection<SpecificPersonVM> Items { get; private set; }
    
            public SpecificPersonsVM()
            {
                Items = new ObservableCollection<SpecificPersonVM>();
            }
        }
    }
    

    Setupcode (code behind, yeah…)

    namespace WpfApplication18
    {
        public partial class MainWindow
        {
            public MainWindow()
            {
                InitializeComponent();
    
                //SetupPersonsVM();
                SetupSpecificPersonsVM();
            }
    
            private void SetupSpecificPersonsVM()
            {
                var vm = new SpecificPersonsVM();
                vm.Items.Add(new SpecificPersonVM { FirstName = "Johny", LastName = "Bravo", Age = 17 });
                vm.Items.Add(new SpecificPersonVM { FirstName = "Dude", LastName = "Gray", Age = 22 });
                vm.Items.Add(new SpecificPersonVM { FirstName = "Scott", LastName = "Thomas", Age = 34 });
                DataContext = vm;
            }
    
            private void SetupPersonsVM()
            {
                var vm = new PersonsVM();
                vm.Items.Add(new PersonVM { FirstName = "John", LastName = "Scott" });
                vm.Items.Add(new PersonVM { FirstName = "Matthew", LastName = "Johnson" });
                DataContext = vm;
            }
        }
    }
    

    You can serve the view different VMs and it will render different things based on this. In my example there is a DataGrid for both VM types but it could have been more different.

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

Sidebar

Related Questions

I have a view model that looks like this public class ViewModelRound2 { public
I have a view model that contains a Product class type and an IEnumerable<
I have a model that looks like this: from django.db import models from django.contrib.auth.models
I have a Controller that currently looks like this: public ActionResult Index() { var
I have a view model that is displayed in a view that uses uploadify.
I am using javascript unobtrusive validation. I have a view model that I am
I have a view with a model that has several fields. When I render
I have a view model with a couple of properties that are not displaying
if i have created a view model and have a partial form that is
I have a model that gets it's view-count updated after it's viewed. This is

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.