{"record":{"id":"37e33b5b5f4a012b","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-37e33b","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.CannotMoveToUnknownPosition);","messagePattern":"throw new InvalidOperationException\\(SR\\.CannotMoveToUnknownPosition\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/EnumerableCollectionView.cs","lineNumber":443,"sourceCode":"                            // replace error message with a better one\n                            throw new InvalidOperationException(SR.Format(SR.AddedItemNotAtIndex, index));\n                        _snapshot.RemoveAt(index);\n                    }\n                    break;\n\n                case NotifyCollectionChangedAction.Replace:\n                    for (int i = args.NewItems.Count - 1, index = args.NewStartingIndex + i; i >= 0; --i, --index)\n                    {\n                        if (!System.Windows.Controls.ItemsControl.EqualsEx(args.OldItems[i], _snapshot[index]))\n                            // replace error message with a better one\n                            throw new InvalidOperationException(SR.Format(SR.AddedItemNotAtIndex, index));\n                        _snapshot[index] = args.NewItems[i];\n                    }\n                    break;\n\n                case NotifyCollectionChangedAction.Move:\n                    if (args.NewStartingIndex < 0)\n                        throw new InvalidOperationException(SR.CannotMoveToUnknownPosition);\n\n                    if (args.OldStartingIndex < args.NewStartingIndex)\n                    {\n                        for (int i = args.OldItems.Count - 1,\n                                oldIndex = args.OldStartingIndex + i,\n                                newIndex = args.NewStartingIndex + i;\n                            i >= 0;\n                            --i, --oldIndex, --newIndex)\n                        {\n                            if (!System.Windows.Controls.ItemsControl.EqualsEx(args.OldItems[i], _snapshot[oldIndex]))\n                                // replace error message with a better one\n                                throw new InvalidOperationException(SR.Format(SR.AddedItemNotAtIndex, oldIndex));\n                            _snapshot.Move(oldIndex, newIndex);\n                        }\n                    }\n                    else\n                    {\n                        for (int i = 0,","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/EnumerableCollectionView.cs#L425-L461","documentation":"When a Move notification arrives with NewStartingIndex < 0, EnumerableCollectionView cannot determine where the item(s) were moved to and throws CannotMoveToUnknownPosition. INotifyCollectionChanged requires Move events to carry a valid destination index.","triggerScenarios":"Source collection raises NotifyCollectionChangedAction.Move with args.NewStartingIndex == -1 (unknown position), processed by ProcessCollectionChanged on a bound view.","commonSituations":"Custom collections implementing Move by reusing a generic constructor or defaulting indices to -1; forwarding a Move event built from incomplete information.","solutions":["Set NewStartingIndex to the post-move index when raising Move events (NotifyCollectionChangedAction.Move accepts a single index).","Raise Reset when the destination index cannot be determined.","Replace the Move with explicit Remove+Add events at known indices.","Use ObservableCollection<T>.Move which always reports the correct index."],"exampleFix":"// before\nRaiseEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, item, -1));\n// after\nRaiseEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, item, newIndex));","handlingStrategy":"validation","validationCode":"if (moveArgs.NewStartingIndex < 0)\n    moveArgs = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);","typeGuard":null,"tryCatchPattern":"try { ApplyMove(args); } catch (InvalidOperationException) { view.Refresh(); }","preventionTips":["Always supply a valid destination index for Move events.","Use ObservableCollection<T>.Move which reports correct indices.","Fall back to Reset when the destination is unknown.","Validate event args in a custom collection base class."],"tags":["wpf","inotifycollectionchanged","move","invalid-index"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}