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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:39:14+00:00 2026-05-20T07:39:14+00:00

Using MVVM, one type of ViewModels include the Model they represnt as a Field.

  • 0

Using MVVM, one type of ViewModels include the Model they represnt as a Field.

So I do have a CompanyModel and a CompanyViewModel that has one instance of CompanyModel.

This CompanyModel has a collection of Divisions belonging to it. So CompanyModel has a List (or some collection class).

Now the CompanyViewModel would want to represent these Divisions as an ObservableCollection<DivisionViewModel>; and you you could add new Divisions in the CompanyViewModel.

What is the best way ensure that the ObservableCollection and the Models collection stay in sync? So when I add a new DivisionViewModel and save it, it automatically saves its model to the CompanyModel’s List<Division>?

I have more classes like this Parent/child relations so I would love something I could reuse or implement perhaps in a AbstractViewModel class.

Note: My ViewModels implement IEditableObject

  • 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-20T07:39:14+00:00Added an answer on May 20, 2026 at 7:39 am

    Probably the easiest way to do this is to create a new class that inherits from ObservableCollection, and which takes a source list and various initialization and mapping functions as parameters. Its signature might look something like this:

    public class SynchronizedObservableCollection<TDest, TSource> : ObservableCollection
    {
        public SynchronizedObservableCollection(
            IList<TSource> source, 
            Func<TSource, TDest> newDestFunc, 
            Func<TDest, TSource> newSourceFunc),
            Func<TSource, TDest, bool> mapSourceToDestFunc
        {
            // Initialize the class here.
        }
    }
    

    You’d then want handle the CollectionChanged event, creating new Source instances when a new Destination instance got added, deleting existing Source instances when an existing Destination instance got deleted, that sort of thing. You’d use the “new” functions above to create new instances of the various entities, and you’d use the “map” functions above in various Linq queries that would allow you to figure out, say, which instance of a viewmodel your ObservableCollection mapped to a model in your List.

    You would use it in your example above like so, perhaps:

    var divisionViewModels = new SynchronizedObservableCollection(
        company.DivisionList, 
        division => new DivisionViewModel(division),
        divisionVm => divisionVm.Model,
        (division, divisionVm) => divisionVm.Model == division);
    

    The exact implementation is left as an exercise to the reader :-). But I’ve used classes like this with some success in previous projects. Just make sure you work up some good unit tests around it, so that you know you can rely on it, and don’t have to spend a lot of time hunting through event-handling callstacks.

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

Sidebar

Related Questions

I recently started using WPF and the MVVM framework, one thing that I have
I have a WPF application using MVVM approach. In one of my viewmodels I
I am using MVVM architecture. I have a usercontrol UC as a View Model
I'm using MVVM and within one of my VM's I have an IsEditable property
I am building an wpf app using MVVM. I have viewModels the employ lazy
I have a WPF/MVVM (using MVVM-Light) app setup with a ComboBox that is inside
I'm starting to creat this project using MVVM Model. But I have no idea
I'm developing a project using the MVVM pattern in WPF. One of the key
I'm using MVVM to bind views to objects in a Tree. I have a
I'm developing a WPF app using MVVM. Most of my views have only xaml

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.