{"record":{"id":"c5751e3df1855b21","repo":"dotnet/wpf","slug":"sr-addeditemnotincollection","errorCode":null,"errorMessage":"SR.AddedItemNotInCollection","messagePattern":"SR\\.AddedItemNotInCollection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":2680,"sourceCode":"            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                    {\n                        --index;        // the new item has been artificially moved elsewhere\n                    }\n                }\n\n                return (index < 0) ? index : index + delta;\n            }\n","sourceCodeStart":2662,"sourceCodeEnd":2698,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L2662-L2698","documentation":"An Add notification did not specify an index (-1), so ListCollectionView searched the source list with IndexOf and did not find the announced item at all. It throws InvalidOperationException with SR.AddedItemNotInCollection because the 'added' item is not actually present in the source.","triggerScenarios":"Raising NotifyCollectionChangedAction.Add with NewStartingIndex == -1 for an item that was never inserted into (or was later removed from) the underlying IList — the IndexOf(item) fallback returns -1.","commonSituations":"Event raised before the actual insert completed; item removed between raise and handling; adding an item to a filtered copy rather than the real source collection; EqualsEx/object identity mismatch so IndexOf can't locate an 'equal' item.","solutions":["Insert the item into the source collection BEFORE raising the Add event.","Raise the event with the actual index instead of -1 to skip IndexOf.","Verify the same object instance (or a value-equal one per the collection's comparer) is in the source list.","If the item genuinely is not in the collection, raise Reset or no event at all."],"exampleFix":"// before (event before mutation)\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, -1));\nItems.Add(item);\n// after\nItems.Add(item);\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, Items.Count - 1));","handlingStrategy":"validation","validationCode":"if (args.Action == NotifyCollectionChangedAction.Add && args.NewStartingIndex == -1 && !sourceList.Contains(args.NewItems[0])) { /* item missing; raise Reset after inserting */ }","typeGuard":null,"tryCatchPattern":"try { /* process add */ } catch (InvalidOperationException) { view.Refresh(); }","preventionTips":["Insert the item into the source list before raising the Add event","Ensure the announced item instance exists in the bound collection","Raise events with an explicit index when the position is known","Never fire Add events for items added to a different collection instance"],"tags":["wpf","collection-view","item-not-found"],"backgroundTag":"entity-not-found","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"}