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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:39:28+00:00 2026-06-17T23:39:28+00:00

I think this answer is the solution to my problem but I am struggling

  • 0

I think this answer is the solution to my problem but I am struggling to understand how to apply it to my problem. Like the other post, I have a two collections I want to keep in sync. My model object has collections of strings:

public class Person {
    public int PersonId {get; set; }
    public string PersonName { get; set; }
    public List<string> PersonNicknames { get; set; }
}

I wrap this model object in its own ViewModel (PersonViewModel). To allow the Nicknames to be edited I also wrap them in their own NicknameViewModel. PersonViewModel then exposes an ObservableCollection<NicknameViewModel> NicknameViewModelCollection which is populated at construction:

        foreach (string stringItem in _person.PersonNicknames)
        {
            var nicknameViewModel = new NicknameViewModel(stringItem);
            this.NicknameViewModelCollection.Add(nicknameViewModel);
        }

When a string is added, removed or changed in PersonViewModel.NicknameViewModelCollection the change is not reflected in the Model collection (i.e. Person.Nicknames). Whenever the user modifies, edits or deletes the string item I need to update the Model collection. I don’t understand how the linked answer works or how to apply it to this problem. An example would be amazing… I’m just at a loss here.

  • 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-17T23:39:30+00:00Added an answer on June 17, 2026 at 11:39 pm

    This is my standard solution for what you are searching for. It has a bit of overhead for your scenario, because it works with a ViewModel type that has a field for it’s context, etc. Anyway, the sollution should become obvious. The collection syncs OneWayToSource in general and TwoWay if the model collection itself is observable. Does this help you? If not, please ask…

    /// <summary>
    /// Observable collection of ViewModels that pushes changes to a related collection of models
    /// </summary>
    /// <typeparam name="TViewModel">Type of ViewModels in collection</typeparam>
    /// <typeparam name="TModel">Type of models in underlying collection</typeparam>
    
    public class VmCollection<TViewModel, TModel> : ObservableCollection<TViewModel>
        where TViewModel : class, IViewModel, new()
        where TModel : class
    
    {
        private readonly object _context;
        private readonly ICollection<TModel> _models;
        private bool _synchDisabled;
    
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="models">List of models to synch with</param>
        /// <param name="context"></param>
        /// <param name="autoFetch">
        /// Determines whether the collection of ViewModels should be
        /// fetched from the model collection on construction
        /// </param>
        public VmCollection(ICollection<TModel> models, object context = null, bool autoFetch = true)
        {
            _models = models;
            _context = context;
    
            // Register change handling for synchronization
            // from ViewModels to Models
            CollectionChanged += ViewModelCollectionChanged;
    
            // If model collection is observable register change
            // handling for synchronization from Models to ViewModels
            if (models is ObservableCollection<TModel>)
            {
                var observableModels = models as ObservableCollection<TModel>;
                observableModels.CollectionChanged += ModelCollectionChanged;
            }
    
    
            // Fecth ViewModels
            if (autoFetch) FetchFromModels();
        }
    
        /// <summary>
        /// CollectionChanged event of the ViewModelCollection
        /// </summary>
        public override sealed event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add { base.CollectionChanged += value; }
            remove { base.CollectionChanged -= value; }
        }
    
        /// <summary>
        /// Load VM collection from model collection
        /// </summary>
        public void FetchFromModels()
        {
            // Deactivate change pushing
            _synchDisabled = true;
    
            // Clear collection
            Clear();
    
            // Create and add new VM for each model
            foreach (TModel model in _models)
                AddForModel(model);
    
            // Reactivate change pushing
            _synchDisabled = false;
        }
    
        private void ViewModelCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // Return if synchronization is internally disabled
            if (_synchDisabled) return;
    
            // Disable synchronization
            _synchDisabled = true;
    
            // Synchronize collection of Models
            if (e.NewItems != null)
                foreach (var v in e.NewItems.OfType<IViewModel<TModel>>())
                    v.AddModelTo(_models);
            if (e.OldItems != null)
                foreach (var v in e.OldItems.OfType<IViewModel<TModel>>())
                    v.RemoveModelFrom(_models);
    
    
            //Enable synchronization
            _synchDisabled = false;
        }
    
        private void ModelCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (_synchDisabled) return;
    
            // Synchronize collection of ViewModels
            if (e.NewItems != null)
                foreach (TModel m in e.NewItems.OfType<TModel>()) this.AddIfNotNull(CreateViewModel(m));
            if (e.OldItems != null) foreach (TModel m in e.OldItems) this.RemoveIfContains(GetViewModelOfModel(m));
        }
    
        private TViewModel CreateViewModel(TModel model)
        {
            return ViewModelCache.Get<TViewModel>.ForExistingModel(model, _context);
        }
    
        private TViewModel GetViewModelOfModel(TModel model)
        {
            return Items.OfType<IViewModel<TModel>>().FirstOrDefault(v => v.IsViewModelOf(model)) as TViewModel;
        }
    
        /// <summary>
        /// Adds a new ViewModel for the specified Model instance
        /// </summary>
        /// <param name="model">Model to create ViewModel for</param>
        public void AddForModel(TModel model)
        {
            Add(CreateViewModel(model));
        }
    
        /// <summary>
        /// Adds a new ViewModel with a new model instance of the specified type,
        /// which is the ModelType or derived from the Model type
        /// </summary>
        /// <typeparam name="TSpecificModel">Type of Model to add ViewModel for</typeparam>
        public void AddNew<TSpecificModel>() where TSpecificModel : TModel, new()
        {
            var m = new TSpecificModel();
            Add(CreateViewModel(m));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is I think a simple problem but not getting the solution yet. I
I think this is a simple question, but I can not find the answer
I think the answer to this is no going in, but I could use
I think I already know the answer to this one, but i hope maybe
I think I already know the answer to this but thought I would ask
I think there is a simple answer to this, but for some reason I
I think i already know the answer to this, but i cannot find anything
Well I think I know the answer to this, but I would appreciate anybody
I have the exact same problem as this post . I want my custom
After two days searching this answer I found a solution. Use GCD for download

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.