/* * Method for synchronization if the collection of the ViewModels changed with the * cases add, remove and reset. */ private void ViewModelCollectionChanged(object bookCollectionVM, NotifyCollectionChangedEventArgs e) { if (synchDisabled) { return; } // Disable synchronization synchDisabled = true; switch (e.Action) { //add a new book to the model collection case NotifyCollectionChangedAction.Add: foreach (var newBook in e.NewItems.OfType <BookViewModel>().Select(v => v.wrappedBook).OfType <Book>()) { bookCollection.Add(newBook); } break; //remove a book from the model collection case NotifyCollectionChangedAction.Remove: foreach (var oldBook in e.OldItems.OfType <BookViewModel>().Select(v => v.wrappedBook).OfType <Book>()) { bookCollection.Remove(oldBook); } break; /* * reset the model collection by clearing the collection and adding the models of the * saved viewmodels. */ case NotifyCollectionChangedAction.Reset: bookCollection.Clear(); foreach (var book in e.NewItems.OfType <BookViewModel>().Select(v => v.wrappedBook).OfType <Book>()) { bookCollection.Add(book); } break; } //Enable synchronization synchDisabled = false; }