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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:56:02+00:00 2026-05-22T15:56:02+00:00

I have a question regarding the MVVM design for C#/WPF. I’ve had a look

  • 0

I have a question regarding the MVVM design for C#/WPF. I’ve had a look at several demo applications, but they didn’t really handle my problem.
My application consists of objects that contain other objects. Much like in a parent – children relationship.

My question now is:

  • does the children attribute have to be a ViewModel
  • and if so, how do I create new Parent objects, which contain the existing Child objects via ViewModels?

I have sth like the following scenario:

class Child {
    string Name;
}

class ChildVM {
    Child _child;
    string Name{return _child.Name;}
}

class Parent {
    string Name;
    List<Child> children;
}

class ParentVM{
    Parent _parent;

    string Name{return _parent.Name;}
    List<ChildVM> children {get;set;}

    ParentVM(Parent p){_parent = p;}
}

void CreateANewParent(){
    List<ChildVM> children = new List<ChildVM>(){new ChildVM(new Child()),...};
    ParentVM parent = new ParentVM(new Parent());
    foreach(ChildVM child in children)
        parent.children.Add(child);
}

The problem here is, that the ParentVM contains the ChildVM, BUT the actual Parent (which is inside of the ParentVM) does not have the Child objects contained by the ChildVM objects. I also don’t think it’s a good idea to duplicate the Child objects, as it leads to redundance and in my application context there is also no need/possibility to create new Child objects.

I also thought about the following class design:

class ParentVM {
    Parent _parent;

    string Name{return _parent.Name;}
    List<Child> children {get{return _parent.Children;}}
}

However, this would mean I would directly operate on the Model if I want to manipulate a ParentVM’s Child object.

On the other hand I could simply leave the (Model) Parent blank and use the ParentVM to create a new Parent in the database. But is this a good way of handling the problem?

  • 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-22T15:56:03+00:00Added an answer on May 22, 2026 at 3:56 pm

    Actually, the proper way to do this is when you first create a ParentVM, you iterate through the children of the passed-in Parent, creating a ChildVM for each, then add those ChildVM objects to the ParentVM’s ChildVMs property. (Some would just call that property ‘Children’ but I personally like being clear it’s a collection of ChildVMs, not a collection of Child objects. Simply adding the ‘VM’ suffix makes that very clear.

    You then also have to listen for change-notification to the actual Parent’s Children collection and update your ChildVMs collection accordingly.

    That way you have a model with Parent->Children->Child and a ViewModel of ParentVM->ChildVMs->ChildVM which is I believe exactly what you want.

    Now I’m also of the belief that you should be able to expose the Parent directly from the ParentVM as well as the Child directly from the ChildVM since your UI might be binding to various properties on those items, such as your Name property above. However M-V-VM purists would say to never do this saying the UI should never know about the model because if the model changes, you have to change the UI. My argument is that if the model changes, you have to change the ViewModel anyway for the exact same reason. The only savings would be if there are several views all sharing the same ViewModel since you’d just have to change it in one place, but realistically, something like ‘Name’ isn’t going to change it’s “name” from the model to the ViewModel so in those cases it’s a non-argument anyway.

    Plus, there is a performance overhead by doing it the ‘purist’ way in that you can’t simply delegate to the model item like you’re doing with Name above because the view won’t ever know about any model-generated changes to the Name property unless you also add all the extra change notification inside the VM, meaning you now have one change notification in the model who’s sole purpose is to fire a second change notification in the VM that then notifies the UI. Pure? Yes. Performance-invasive? You bet, especially when large amounts of changes are going on and you’re using the INotifyPropertyChanged interface because that means you have to do string comparisons in the change handler to detect and delegate all those changes! If you bind directly to ParentVM.Parent.Name property however, you would already have that change notification from the model to notify the UI and you also keep your VM clean for things that are only VM- or View-specific.

    What I never do however is place anything in the Model that is view-only info. THAT to me is what the ViewModel is for. So for instance, if the children have a specific color based on an enum or whatever, that to me is something that goes in the ChildVM and not the Model itself, and if there are any properties in the model that dictate that color, like a property of that enum, in that case, yes, I would wire up change-notifications from the model inside the ChildVM. (Truthfully, I may even just do it via a color converter in the UI directly, still binding to the model’s enum. It’s really a case-by-case thing.)

    HTH,

    Mark

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

Sidebar

Related Questions

I have a question regarding how to best accomplish something in WPF MVVM. I
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have a question regarding handling errors in a J2EE application. Our current application
I'm writing a small webapp in Grails and I have the following question regarding
Following on from my recent question regarding parsing XML files in Java I have
I have a question about best practices regarding how one should approach storing complex
I have a simple question and wish to hear others' experiences regarding which is
I actually have two questions regarding the same problem but I think it is
Background I have a TreeView that follows the MVVM design pattern and supports multiple

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.