{"record":{"id":"b230a742b7c0d101","repo":"dotnet/wpf","slug":"sr-cannotmovetounknownposition-collectionview","errorCode":null,"errorMessage":"SR.CannotMoveToUnknownPosition","messagePattern":"SR\\.CannotMoveToUnknownPosition","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs","lineNumber":1971,"sourceCode":"                    break;\n\n                case NotifyCollectionChangedAction.Remove:\n                    if (e.OldItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    if (e.OldStartingIndex < 0)\n                        throw new InvalidOperationException(SR.RemovedItemNotFound);\n                    break;\n\n                case NotifyCollectionChangedAction.Replace:\n                    if (e.NewItems.Count != 1 || e.OldItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    break;\n\n                case NotifyCollectionChangedAction.Move:\n                    if (e.NewItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    if (e.NewStartingIndex < 0)\n                        throw new InvalidOperationException(SR.CannotMoveToUnknownPosition);\n                    break;\n\n                case NotifyCollectionChangedAction.Reset:\n                    break;\n\n                default:\n                    throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, e.Action));\n            }\n        }\n\n        // fix up CurrentPosition and CurrentItem after a collection change\n        private void AdjustCurrencyForAdd(int index)\n        {\n            // adjust current index if insertion is earlier\n            if (Count == 1)\n                _currentPosition = -1;\n            else if (index <= _currentPosition)\n            {","sourceCodeStart":1953,"sourceCodeEnd":1989,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs#L1953-L1989","documentation":"When a source collection raises a Move event, CollectionView requires a valid target index. If NewStartingIndex is negative (unknown, -1), the view cannot place the moved item and throws InvalidOperationException with CannotMoveToUnknownPosition.","triggerScenarios":"A bound collection raises NotifyCollectionChangedAction.Move with NewStartingIndex = -1, typically from a custom collection that constructs the args without specifying the new index.","commonSituations":"Custom observable collections raising Move events with default/unknown index arguments; refactored collection code where the new position was computed as -1 on failure.","solutions":["Ensure Move events are raised with a valid non-negative NewStartingIndex","If the new position is unknown, raise Reset instead","Assert/validate the computed target index before raising the event"],"exampleFix":"// before\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, item, -1, oldIndex));\n// after\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));","handlingStrategy":"validation","validationCode":"bool isValidMoveIndex(NotifyCollectionChangedEventArgs e) => e.Action != NotifyCollectionChangedAction.Move || e.NewStartingIndex >= 0;","typeGuard":"static bool HasKnownMoveTarget(NotifyCollectionChangedEventArgs e) => e.Action == NotifyCollectionChangedAction.Move && e.NewStartingIndex >= 0;","tryCatchPattern":"try { view.ProcessChange(e); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"CannotMoveToUnknownPosition\")) { view.Refresh(); }","preventionTips":["Always supply the destination index in Move notifications","Use Reset when the target position is unknown","Validate computed indices before raising events"],"tags":["wpf","data-binding","collectionview","invalid-argument-value"],"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-22T01:17:13.364Z"}