{"record":{"id":"e9b2b8192f5783e5","repo":"dotnet/wpf","slug":"sr-selectionrequired-windowslistviewitem","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/WindowsListViewItem.cs","lineNumber":468,"sourceCode":"            }\n\n            // simple case: item is not selected\n            if (!WindowsListView.IsItemSelected (_hwnd, _item))\n            {\n                return;\n            }\n\n            // object does not support multi-selection\n            if (!WindowsListView.MultiSelected (_hwnd))\n            {\n                IRawElementProviderSimple container = ((ISelectionItemProvider)this).SelectionContainer;\n                bool selectionRequired = container != null ? ((ISelectionProvider)container).IsSelectionRequired : true;\n\n                // For single selection containers that IsSelectionRequired == false a\n                // RemoveFromSelection is valid.\n                if (selectionRequired)\n                {\n                    throw new InvalidOperationException(SR.SelectionRequired);\n                }\n            }\n\n            // try to unselect the item\n            if (!WindowsListView.UnSelectItem(_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 WindowsListView.IsItemSelected (_hwnd, _item);\n            }\n        }","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs#L450-L486","documentation":"In ISelectionItemProvider.RemoveFromSelection, when the container's ISelectionProvider.IsSelectionRequired is true (a single-selection list that must always have a selection, as typical for non-multiselect ListViews), removing the current selection is invalid and the provider throws InvalidOperationException(SR.SelectionRequired). The UIA contract forbids leaving a selection-required container with no selection.","triggerScenarios":"Calling RemoveFromSelection on an item of a ListView whose container reports IsSelectionRequired == true - the common case for single-select ListView controls without LVS_MULTISELECT combined with the provider's default assumption.","commonSituations":"Automation scripts attempting to clear selection on a single-select ListView; generic 'deselect all' logic applied uniformly to single- and multi-select lists; behavior differences across framework versions in how IsSelectionRequired is reported.","solutions":["Check the container's SelectionPattern.IsSelectionRequired before calling RemoveFromSelection and skip the call when true.","Replace the deselection with Select() on the item you want to be the (only) selection instead.","If clearing the selection is a requirement, make the ListView multi-select (LVS_MULTISELECT) and/or set IsSelectionRequired=false semantics in the app.","Catch InvalidOperationException around RemoveFromSelection and treat 'selection required' as a benign no-op in cleanup code."],"exampleFix":"// before\nselItem.RemoveFromSelection(); // throws on single-select lists\n\n// after\nvar sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);\nif (!sel.Current.IsSelectionRequired)\n    selItem.RemoveFromSelection();\nelse\n    selItem.Select(); // replace selection instead","handlingStrategy":"validation","validationCode":"var sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);\nif (sel.Current.IsSelectionRequired)\n    throw new InvalidOperationException(\"Container requires a selection; RemoveFromSelection is invalid.\");","typeGuard":"bool CanRemoveSelection(AutomationElement list) =>\n    list.TryGetCurrentPattern(SelectionPattern.Pattern, out var p) && !((SelectionPattern)p).Current.IsSelectionRequired;","tryCatchPattern":"try { selItem.RemoveFromSelection(); }\ncatch (InvalidOperationException)\n{ selItem.Select(); /* selection-required list: replace instead of remove */ }","preventionTips":["Check SelectionPattern.IsSelectionRequired before RemoveFromSelection","For single-select lists, model 'deselect' as Select() of the next desired item","Treat this exception as benign in cleanup/teardown code","Keep automation helpers capability-aware instead of assuming multi-select semantics"],"tags":["uiautomation","wpf","listview","selection","single-select"],"backgroundTag":"selection-required","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"}