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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:44:16+00:00 2026-05-21T12:44:16+00:00

I have a service returning an array of type Party. Party has two subtypes,

  • 0

I have a service returning an array of type Party. Party has two subtypes, Person and Organization. I’m consuming this service in my WPF application (Prism, MVVM) from a view model. In the constructor of this view model I populate an observable collection of type Party:

public PhoneBookViewModel(IPhoneBookService phoneBookProxy)
{
    _phoneBookProxy = phoneBookProxy;

    var parties = _phoneBookProxy.GetAllParties();
    _parties = new ObservableCollection<Party>(parties.ToList());
}

So far so good. In my PhoneBookView I have an ItemsControl that binds to this collection. In this control I want to render each Party by using another View (and its view model). So when Party is of type Person, inject PersonView and pass the Party object to the constructor of the PersonViewModel, and when Party is of type Organization, render OrganizationView, and so on… You get the picture (or?).

But I can’t figure out how to do this in XAML. Any ideas?
This is probaly not the best way of doing it, so if you can recommend a better approach, please enlighten me 🙂

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-21T12:44:17+00:00Added an answer on May 21, 2026 at 12:44 pm

    Lets examine this from the view towards the model:


    Lets assume we have 2 different types of views, 1 type of view model:

    ViewA –> Created within an items control using DataTempate/DataTemplateSelector, Binded > to ViewModelA

    ViewB –> Created within an items control using DataTempate/DataTemplateSelector, Binded to ViewModelA

    If both views are binded to the same view model, you would end up with the same view.


    Lets try again with 2 different types of views and 2 different types of view models:

    ViewA –> Created within an items control using DataTempate/DataTemplateSelector, Binded to ViewModelA –> Binded to ModelA

    ViewB –> Created within an items control using DataTempate/DataTemplateSelector, Binded to ViewModelB –> Binded to ModelB

    This IS possible.


    Now if you model your view-models and models like this (pseudo code):

    public PhoneBookViewModel
    {
        public PhoneBookViewModel()
        {
            _parties = new ObservalbeCollection<PartyViewModel>();
        }
    
        private PhoneBook _dataContext;
    
        // This is the property the VM uses to access the model
        public PhoneBook DataContext
        {
            get { return _dataContext; }
            set
            {
                if (_dataContext != null)
                {
                    _dataContext.Parties.CollectionChanged -= OnModelPartiesChanged;
                }
                _dataContext = value;
                if (_dataContext != null)
                {
                    _dataContext.Parties.CollectionChanged += OnModelPartiesChanged;
                }
            }
        }
    
        private ObservableCollection<PartyViewModel> _parties;
    
        // This is the property the view uses to access the collection of VM parties
        public ObservableCollection<PartyViewModel> PartiesViewModels { get { return _parties; } }
    
        private void OnModelPartiesChanged(...)
        {
            // Add/remove VMs to/from PartiesViewModels here
        }
    }
    
    // Model
    public PhoneBook
    {
        public PhoneBook()
        {
            _parties = new ObservalbeCollection<Party>();
        }
    
        private ObservableCollection<Party> _parties;
    
        // This is the property the VM uses to access the model's parties
        public ObservableCollection<Party> Parties { get { return _parties; } }
    }
    
    public PersonViewModel : PartyViewModel
    {
        new Person DataContext { get; set; }
    }
    
    public PartyViewModel
    {
        public Party DataContext { get; set; }
    }
    

    then you will get correct type of VMs for each model item,
    view will be binded to VM items, not model items.


    View’s datatemplates:

    <DataTemplate x:Target={x:Type myVmNamespace:PersonViewModel}">
        <PersonView/>
    </DataTemplate>
    
    <DataTemplate x:Target={x:Type myVmNamespace:GroupViewModel}">
        <GroupView/>
    </DataTemplate>
    

    View’s itemscontrol:

    <!-- Bind to Parties property of PhoneBookVM -->
    <!-- Uses datatemplates for items -->
    <ListView ItemsSource={Binding Parties}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.