{"record":{"id":"cc177f042a07258d","repo":"dotnet/wpf","slug":"sr-selectionrequired","errorCode":null,"errorMessage":"SR.SelectionRequired","messagePattern":"SR\\.SelectionRequired","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs","lineNumber":887,"sourceCode":"                // This state could change anytime\n                if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n                {\n                    throw new ElementNotEnabledException();\n                }\n\n                if (!IsSelected(_hwnd, _item))\n                {\n                    // simple case, item is not selected\n                    return;\n                }\n\n                // object does not support multi-selection\n                if (!_listBox.IsMultipleSelection())\n                {\n                    // single-selected lb - user cannot remove the selection using keyboard and mouse\n                    // At this point we know that item is selected, lb is single-selected hence\n                    // RemoveFromSelection is not possible\n                    throw new InvalidOperationException(SR.SelectionRequired);\n                }\n\n                if (!UnSelect(_hwnd, _item))\n                {\n                    throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                }\n            }\n\n            // True if this element is part of the the selection\n            bool ISelectionItemProvider.IsSelected\n            {\n                get\n                {\n                    return ListboxItem.IsSelected (_hwnd, _item);\n                }\n            }\n\n            // Returns the container for this element","sourceCodeStart":869,"sourceCodeEnd":905,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs#L869-L905","documentation":"RemoveFromSelection throws InvalidOperationException(SR.SelectionRequired) when the listbox is single-selection (IsMultipleSelection false): the item is selected and a single-selection listbox requires exactly one selection, so the provider refuses to remove it — the user could not do this with keyboard/mouse either.","triggerScenarios":"Calling RemoveFromSelection on the (only) selected item of a single-selection Win32 listbox.","commonSituations":"Automation attempting to clear a single-select listbox; assuming UIA exposes deselect on all list items; porting automation written for multi-select or WPF ListBox.","solutions":["Select a different item instead of removing the selection.","Skip RemoveFromSelection when SelectionPattern.CanSelectMultiple is false.","Clear via app-specific means (e.g. set selected index to -1 through the app's own API) if the app supports it."],"exampleFix":"// before\nselectionItem.RemoveFromSelection();\n// after\nvar sel = listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;\nif (sel.Current.CanSelectMultiple)\n{\n    selectionItem.RemoveFromSelection();\n}","handlingStrategy":"validation","validationCode":"var sp = listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;\nif (sp == null || !sp.Current.CanSelectMultiple) { /* do not call RemoveFromSelection */ }","typeGuard":"bool CanRemoveSelection(AutomationElement item, AutomationElement listbox) => (listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern)?.Current.CanSelectMultiple == true;","tryCatchPattern":"try { selectionItem.RemoveFromSelection(); }\ncatch (InvalidOperationException) { /* selection required: select a different item instead */ }","preventionTips":["Only call RemoveFromSelection on multi-select listboxes","Select a replacement item instead of deselecting in single-select lists","Check CanSelectMultiple before any selection manipulation"],"tags":["uiautomation","single-selection","selection-required"],"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"}