{"record":{"id":"5b569a6f4f115cf0","repo":"dotnet/wpf","slug":"sr-addeditemnotatindex-formatted-with-index","errorCode":null,"errorMessage":"SR.AddedItemNotAtIndex (formatted with index)","messagePattern":"SR\\.AddedItemNotAtIndex \\(formatted with index\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":2673,"sourceCode":"                return (NewItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning)\n                        ? 1 : UsesLocalArray ? InternalCount - 2 : index;\n            }\n\n            int delta = IsGrouping ? 0 :\n                        (NewItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning)\n                        ? (IsAddingNew ? 2 : 1) : 0;\n            IList ilFull = (AllowsCrossThreadChanges ? ShadowCollection : SourceCollection) as IList;\n\n            // validate input\n            if (index < -1 || index > ilFull.Count)\n                throw new InvalidOperationException(SR.Format(SR.CollectionChangeIndexOutOfRange, index, ilFull.Count));\n\n            if (action == NotifyCollectionChangedAction.Add)\n            {\n                if (index >= 0)\n                {\n                    if (!System.Windows.Controls.ItemsControl.EqualsEx(item, ilFull[index]))\n                        throw new InvalidOperationException(SR.Format(SR.AddedItemNotAtIndex, index));\n                }\n                else\n                {\n                    // event didn't specify index - determine it the hard way\n                    index = ilFull.IndexOf(item);\n                    if (index < 0)\n                        throw new InvalidOperationException(SR.AddedItemNotInCollection);\n                }\n            }\n\n            // if there's no sort or filter, use the index into the full array\n            if (!UsesLocalArray)\n            {\n                if (IsAddingNew)\n                {\n                    if (NewItemPlaceholderPosition != NewItemPlaceholderPosition.None &&\n                        index > _newItemIndex)\n                    {","sourceCodeStart":2655,"sourceCodeEnd":2691,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L2655-L2691","documentation":"An Add notification supplied an index, but verification found the item at that index in the source list is not the announced new item (reference/equality check via ItemsControl.EqualsEx fails). ListCollectionView throws InvalidOperationException with SR.AddedItemNotAtIndex.","triggerScenarios":"Raising NotifyCollectionChangedAction.Add with NewStartingIndex i where ilFull[i] does not equal the NewItems[0] object — e.g. event raised with a stale index, or the item was inserted at a different position than reported.","commonSituations":"Sorted/filtered custom collections whose insertion position differs from the reported index; event args reused or cached with an outdated index; double insertion where the second insert shifts indices.","solutions":["Pass the true index where the item now sits in the source list, verified after the actual insert.","Pass -1 as NewStartingIndex to let the view locate the item via IndexOf.","Ensure EqualsEx semantics: use the same instance or value-equal item in the event as inserted."],"exampleFix":"// before (index guessed)\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, sortedPositionGuess));\n// after\nvar actualIndex = Items.IndexOf(item);\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, actualIndex));","handlingStrategy":"validation","validationCode":"if (args.Action == NotifyCollectionChangedAction.Add && args.NewStartingIndex >= 0 && !ReferenceEquals(sourceList[args.NewStartingIndex], args.NewItems[0])) { /* index mismatch; use Reset */ }","typeGuard":null,"tryCatchPattern":"try { /* process add */ } catch (InvalidOperationException) { view.Refresh(); }","preventionTips":["Determine the actual post-insert index (IndexOf) before raising the event","Pass -1 to let the view locate the item when unsure","Raise events only after the list mutation is committed"],"tags":["wpf","collection-view","index-mismatch"],"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"}