{"record":{"id":"9c0686a15d203155","repo":"dotnet/wpf","slug":"sr-insertindeferselectionactive","errorCode":null,"errorMessage":"SR.InsertInDeferSelectionActive","messagePattern":"SR\\.InsertInDeferSelectionActive","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedItemCollection.cs","lineNumber":86,"sourceCode":"                }\n            }\n        }\n\n        /// <summary>\n        /// Inserts an item in the selection\n        /// </summary>\n        protected override void InsertItem(int index, object item)\n        {\n            if (_updatingSelectedItems)\n            {\n                // For defered selection we should allow only Add method\n                if (index == Count)\n                {\n                    _selector.SelectionChange.Select(_selector.NewItemInfo(item), true /* assumeInItemsCollection */);\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.InsertInDeferSelectionActive);\n                }\n            }\n            else\n            {\n                using (ChangeSelectedItems())\n                {\n                    base.InsertItem(index, item);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Sets an item on specified index\n        /// </summary>\n        protected override void SetItem(int index, object item)\n        {\n            if (_updatingSelectedItems)\n            {","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedItemCollection.cs#L68-L104","documentation":"SelectedItemCollection.InsertItem throws when an item is inserted into the SelectedItems collection while the deferred selection update (ChangeSelectedItems/SelectionChange) is active. During a deferred selection pass, only queued Add/Remove of ItemInfo is allowed; inserting at an arbitrary index mid-update is not supported and would corrupt the selection commit.","triggerScenarios":"Calling SelectedItems.Insert(item, index) (with index == Count it routes through Select, otherwise throws) from inside an UpdateSelectedItems block or while a SelectionChange is in progress (e.g. inside a SelectionChanged handler or OnExecutedDelete/MakeFullRowSelection).","commonSituations":"Manipulating ListBox.SelectedItems inside SelectionChanged event handlers; custom selection logic that calls Insert during a BeginUpdateSelectedItems/EndUpdateSelectedItems block.","solutions":["Move the Insert call outside the deferred-selection block / SelectionChanged handler, deferring with Dispatcher.BeginInvoke if needed.","Use SelectedItems.Add instead of Insert at a specific index during selection updates.","End any active UpdateSelectedItems scope before inserting.","Wrap modifications in Selector.UpdateSelectedItems which manages the begin/end scope correctly."],"exampleFix":"// before\nlistBox.SelectedItems.Insert(0, item); // inside SelectionChanged\n// after\nDispatcher.BeginInvoke(new Action(() => listBox.SelectedItems.Insert(0, item)));","handlingStrategy":"try-catch","validationCode":"if (!listBox.IsUpdatingSelectedItems && !selectionChangeActive) listBox.SelectedItems.Insert(0, item);","typeGuard":null,"tryCatchPattern":"try { listBox.SelectedItems.Insert(0, item); }\ncatch (InvalidOperationException) { Dispatcher.BeginInvoke(new Action(() => listBox.SelectedItems.Insert(0, item))); }","preventionTips":["Don't mutate SelectedItems inside SelectionChanged handlers.","Defer mutations with Dispatcher.BeginInvoke.","Use UpdateSelectedItems for programmatic selection.","Never call Insert during a deferred selection scope."],"tags":["wpf","selector","selection","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}