CompletionVM GetExistingCompletionVM(Completion completion) { if (completion == null) { return(null); } var vm = CompletionVM.TryGet(completion); Debug.Assert(vm != null); return(vm); }
void CompletionList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { int i; switch (e.Action) { case NotifyCollectionChangedAction.Add: Debug.Assert(e.NewItems != null); i = e.NewStartingIndex; var newList = new List <CompletionVM>(); foreach (Completion c in e.NewItems) { var vm = GetOrCreateVM(c); newList.Add(vm); list.Insert(i++, vm); } CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newList, e.NewStartingIndex)); break; case NotifyCollectionChangedAction.Remove: Debug.Assert(e.OldItems != null); var oldList = new List <CompletionVM>(); foreach (Completion c in e.OldItems) { var vm = CompletionVM.TryGet(c); oldList.Add(vm); Debug.Assert(list[e.OldStartingIndex].Completion == vm?.Completion); list.RemoveAt(e.OldStartingIndex); } CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldList, e.NewStartingIndex)); break; case NotifyCollectionChangedAction.Replace: throw new NotSupportedException(); case NotifyCollectionChangedAction.Move: throw new NotSupportedException(); case NotifyCollectionChangedAction.Reset: ReinitializeList(); CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); break; default: Debug.Fail($"Unknown action: {e.Action}"); break; } }
CompletionVM GetOrCreateVM(Completion completion) => CompletionVM.TryGet(completion) ?? new CompletionVM(completion, imageMonikerService);