private void ChildVMCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (SynchDisabled) { return; } SynchDisabled = true; switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (object item in e.NewItems) { var mItem = (ChildVM)item; int idx = ChildMCollection.IndexOf(mItem); // the operation could have been either Insert or Add if (0 <= idx && idx <= ChildVMCollection.Count - 1) { ChildMCollection.Insert(idx, mItem); } else { ChildMCollection.Add(mItem); } } break; case NotifyCollectionChangedAction.Remove: foreach (object item in e.OldItems) { var vmItem = item as ChildVM; // find VM objects that wrap the relevant model object and remove them IEnumerable <ChildM> query; while ((query = from vm in ChildMCollection where vm.Text == vmItem.Text select vm).Count() > 0) { ChildM mItem = query.First(); int index = ChildMCollection.IndexOf(vmItem); ChildMCollection.Remove(vmItem); // TODO: what if I implement the == or != operator? } } break; case NotifyCollectionChangedAction.Reset: ChildMCollection.Clear(); break; case NotifyCollectionChangedAction.Move: // TODO: handle multiple items ChildMCollection.Move(e.OldStartingIndex, e.NewStartingIndex); break; case NotifyCollectionChangedAction.Replace: // TODO: handle multiple items ChildMCollection[e.OldStartingIndex] = (ChildM)e.NewItems[0]; break; default: throw new NotImplementedException(); } SynchDisabled = false; }
public ChildVM(ChildM m) { Text = m.Text; }