{"record":{"id":"25073f0ca706f0e5","repo":"dotnet/wpf","slug":"sr-changingcollectionnotsupported","errorCode":null,"errorMessage":"SR.ChangingCollectionNotSupported","messagePattern":"SR\\.ChangingCollectionNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Selector.cs","lineNumber":862,"sourceCode":"                        SelectionChange.Cancel();\n                    }\n                }\n            }\n\n            return succeeded;\n        }\n\n        private void OnSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)\n        {\n            // Ignore selection changes we're causing.\n            if (SelectionChange.IsActive)\n            {\n                return;\n            }\n\n            if (!CanSelectMultiple)\n            {\n                throw new InvalidOperationException(SR.ChangingCollectionNotSupported);\n            }\n\n            SelectionChange.Begin();\n            bool succeeded=false;\n            try\n            {\n                switch (e.Action)\n                {\n                    case NotifyCollectionChangedAction.Add:\n                        if (e.NewItems.Count != 1)\n                            throw new NotSupportedException(SR.RangeActionsNotSupported);\n\n                        SelectionChange.Select(NewUnresolvedItemInfo(e.NewItems[0]), false /* assumeInItemsCollection */);\n                        break;\n\n                    case NotifyCollectionChangedAction.Remove:\n                        if (e.OldItems.Count != 1)\n                            throw new NotSupportedException(SR.RangeActionsNotSupported);","sourceCodeStart":844,"sourceCodeEnd":880,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Selector.cs#L844-L880","documentation":"Selector throws this InvalidOperationException when code modifies the SelectedItems collection while the Selector is in single-selection mode (CanSelectMultiple is false, i.e. SelectionMode is Single). In single-selection mode SelectedItems is maintained by the Selector itself and must not be directly mutated, because any collection change would allow multiple selections, contradicting the mode. The library throws inside OnSelectedItemsCollectionChanged as soon as it observes a direct change to the collection.","triggerScenarios":"Calling SelectedItems.Add/Remove/Clear (or any mutation) on a ListBox/Selector whose SelectionMode is Single; assigning a mutated collection in XAML or code; data-binding code that writes into SelectedItems before switching to Multiple/Extended.","commonSituations":"Developers porting code that worked with a multi-select ListBox to a single-select one; MVVM code that manipulates SelectedItems directly; setting SelectionMode=Single after populating SelectedItems in XAML.","solutions":["Switch the control to SelectionMode=\"Multiple\" or \"Extended\" before mutating SelectedItems","Set the selection via the item's IsSelected property or SelectedItem/SelectedIndex instead of mutating SelectedItems","If the intent is only to change the current selection in single mode, use SelectedItem = value"],"exampleFix":"// before\nlistBox.SelectedItems.Add(item); // SelectionMode is Single -> throws\n// after\nlistBox.SelectionMode = SelectionMode.Multiple;\nlistBox.SelectedItems.Add(item);","handlingStrategy":"validation","validationCode":"if (!listBox.CanSelectMultiple)\n    throw new InvalidOperationException(\"Switch SelectionMode to Multiple/Extended before mutating SelectedItems.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    listBox.SelectedItems.Add(item);\n}\ncatch (InvalidOperationException ex)\n{\n    // fall back: set SelectionMode first or use SelectedItem\n}","preventionTips":["Check Selector.CanSelectMultiple before any SelectedItems mutation","Prefer item.IsSelected / SelectedItem over mutating SelectedItems in single-selection mode","Don't bind SelectedItems in XAML for single-select controls"],"tags":["wpf","selector","selectionmode","invalidoperation"],"backgroundTag":"unsupported-operation","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"}