{"record":{"id":"59843756fcda914e","repo":"dotnet/wpf","slug":"sr-deferselectionactive","errorCode":null,"errorMessage":"SR.DeferSelectionActive","messagePattern":"SR\\.DeferSelectionActive","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedItemCollection.cs","lineNumber":188,"sourceCode":"\n            private SelectedItemCollection _owner;\n        }\n\n        private int _changeCount;\n        private Changer _changer;\n\n        #endregion Reentrant changes\n\n        #region MultiSelector methods\n\n        /// <summary>\n        /// Begin tracking selection changes. SelectedItems.Add/Remove will queue up the changes but not commit them until EndUpdateSelecteditems is called.\n        /// </summary>\n        internal void BeginUpdateSelectedItems()\n        {\n            if (_selector.SelectionChange.IsActive || _updatingSelectedItems)\n            {\n                throw new InvalidOperationException(SR.DeferSelectionActive);\n            }\n            _updatingSelectedItems = true;\n            _selector.SelectionChange.Begin();\n        }\n\n        /// <summary>\n        /// Commit selection changes.\n        /// </summary>\n        internal void EndUpdateSelectedItems()\n        {\n            if (!_selector.SelectionChange.IsActive || !_updatingSelectedItems)\n            {\n                throw new InvalidOperationException(SR.DeferSelectionNotActive);\n            }\n            _updatingSelectedItems = false;\n            _selector.SelectionChange.End();\n        }\n","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/SelectedItemCollection.cs#L170-L206","documentation":"BeginUpdateSelectedItems starts a deferred selection scope in which SelectedItems.Add/Remove are queued until EndUpdateSelectedItems. It throws InvalidOperationException if a selection change is already active (_selector.SelectionChange.IsActive) or a deferred update is already in progress (_updatingSelectedItems), preventing nested/overlapping selection transactions.","triggerScenarios":"Calling BeginUpdateSelectedItems (or Selector.UpdateSelectedItems) while another SelectionChange is active — e.g. calling it from within a SelectionChanged handler, from OnSelectedCellsChanged, or nesting one UpdateSelectedItems call inside another.","commonSituations":"Reentrancy: modifying selection inside SelectionChanged which itself runs during a selection change; a helper method calling UpdateSelectedItems while the caller already holds the deferred scope.","solutions":["Ensure Begin/End pairs are balanced and never nested; add a bool guard around reentrant calls.","Don't start a deferred update from inside SelectionChanged; defer with Dispatcher.BeginInvoke.","Consolidate selection logic into a single UpdateSelectedItems scope.","Track scope ownership (e.g. ref-count or flag) in helper code shared by multiple event handlers."],"exampleFix":"// before\nvoid OnSelectionChanged(...) { listBox.UpdateSelectedItems(() => { ... }); }\n// after\nvoid OnSelectionChanged(...) { Dispatcher.BeginInvoke(new Action(() => listBox.UpdateSelectedItems(() => { ... }))); }","handlingStrategy":"validation","validationCode":"if (!isUpdatingSelectedItems && !selectionChangeActive) StartDeferredSelection();","typeGuard":"bool canBeginUpdate = !listBox.IsSelectionChangeActive;","tryCatchPattern":"try { BeginUpdateSelectedItems(); ... }\ncatch (InvalidOperationException) { /* already active: join or defer the existing scope */ }","preventionTips":["Never nest UpdateSelectedItems scopes.","Guard reentrant calls with a boolean flag.","Don't begin deferred selection from SelectionChanged; defer via Dispatcher.","Centralize selection updates in one code path."],"tags":["wpf","selector","selection","reentrancy"],"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"}