{"record":{"id":"55d36efe0ef65eb3","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-55d36e","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/CompositeCollectionView.cs","lineNumber":1451,"sourceCode":"                    if (e.NewItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    break;\n\n                case NotifyCollectionChangedAction.Remove:\n                    if (e.OldItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\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        /// <summary>\n        /// Helper to raise a PropertyChanged event  />).\n        /// </summary>\n        private void OnPropertyChanged(string propertyName)\n        {\n            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));\n        }\n","sourceCodeStart":1433,"sourceCodeEnd":1469,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs#L1433-L1469","documentation":"A Move notification with a negative NewStartingIndex is invalid: the view cannot move an item to an unknown position. ValidateCollectionChangedEventArgs throws InvalidOperationException(SR.CannotMoveToUnknownPosition) in that case.","triggerScenarios":"A source collection fires CollectionChanged with Action=Move and NewStartingIndex < 0 (e.g. -1 from a MoveRange helper that failed to compute the target).","commonSituations":"Custom move implementations passing -1 when the target index could not be found; sort/reorder code that forwards an unset index.","solutions":["Fix the source to always supply a valid non-negative NewStartingIndex for Move events.","Validate the target index before raising Move; if unknown, raise Remove + Add at the correct position instead.","Assert index >= 0 in your move helper before constructing NotifyCollectionChangedEventArgs."],"exampleFix":"// before\nint idx = list.IndexOf(item); // -1 when missing\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(Move, item, idx));\n\n// after\nint idx = list.IndexOf(item);\nif (idx >= 0)\n    OnCollectionChanged(new NotifyCollectionChangedEventArgs(Move, item, idx));","handlingStrategy":"validation","validationCode":"if (e.Action == NotifyCollectionChangedAction.Move && e.NewStartingIndex < 0)\n    throw new InvalidOperationException(\"Move requires a non-negative NewStartingIndex\");","typeGuard":null,"tryCatchPattern":"try { view.ProcessCollectionChanged(e); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"move\"))\n{ RebuildView(); }","preventionTips":["Always compute and pass a valid target index when raising Move.","Use Remove+Add instead of Move when the target position is unknown.","Assert NewStartingIndex >= 0 in move helpers."],"tags":["wpf","collection-view","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-22T01:17:13.364Z"}