{"record":{"id":"beddd31ce108f76d","repo":"dotnet/wpf","slug":"sr-doesnotsupportmultipleselection-windowslistviewitem","errorCode":null,"errorMessage":"SR.DoesNotSupportMultipleSelection","messagePattern":"SR\\.DoesNotSupportMultipleSelection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs","lineNumber":430,"sourceCode":"            }\n\n            // simple case: object already 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 and nothing is selected\n                // an AddToSelection is valid.\n                if (selectionRequired || WindowsListView.GetSelectedItemCount(_hwnd) > 0)\n                {\n                    throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);\n                }\n            }\n\n            // At this point we know: Item either supports multiple selection or nothing\n            // is selected in the list\n            // Try to select an item\n            if (!WindowsListView.SelectItem(_hwnd, _item))\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n        }\n\n        // Removes this element from the selection\n        void ISelectionItemProvider.RemoveFromSelection ()\n        {\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs#L412-L448","documentation":"In ISelectionItemProvider.AddToSelection, for single-selection containers the provider checks the parent ISelectionProvider.IsSelectionRequired and the current selected-item count; if the container requires a selection or already has one selected, adding a second item is invalid, so it throws InvalidOperationException(SR.DoesNotSupportMultipleSelection). This enforces the UIA rule that AddToSelection must not create a multi-selection in a single-select list.","triggerScenarios":"Calling AddToSelection on a ListView item whose container has IsMultipleSelection == false (e.g. default single-select ListView without LVS_MULTISELECT) while IsSelectionRequired is true or GetSelectedItemCount > 0.","commonSituations":"Automation scripts written for multi-select lists being reused against single-select ListViews; a regression where the app removed the LVS_MULTISELECT style; selecting a second item with Ctrl+click semantics simulated via UIA on a single-select control.","solutions":["Check the container's SelectionPattern.CanSelectMultiple property before calling AddToSelection; use Select() (replace selection) for single-select lists.","If multi-select behavior is needed, fix the ListView to include LVS_MULTISELECT in the app's control style.","Simulate multi-select via keyboard input (SendKeys Ctrl+Click) instead of the SelectionItem pattern on single-select lists.","Catch InvalidOperationException and fall back to Select() on the target item when the list is single-select."],"exampleFix":"// before\nselItem.AddToSelection(); // throws on single-select lists\n\n// after\nvar sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);\nif (sel.Current.CanSelectMultiple)\n    selItem.AddToSelection();\nelse\n    selItem.Select();","handlingStrategy":"validation","validationCode":"var sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);\nif (!sel.Current.CanSelectMultiple && (sel.Current.GetSelection()?.Length ?? 0) > 0)\n    throw new InvalidOperationException(\"Single-select list already has a selection; use Select() instead of AddToSelection().\");","typeGuard":"bool SupportsMultiSelect(AutomationElement list) =>\n    list.TryGetCurrentPattern(SelectionPattern.Pattern, out var p) && ((SelectionPattern)p).Current.CanSelectMultiple;","tryCatchPattern":"try { selItem.AddToSelection(); }\ncatch (InvalidOperationException) { selItem.Select(); } // fall back to replace-selection on single-select lists","preventionTips":["Check SelectionPattern.CanSelectMultiple on the container before AddToSelection","Use Select() for single-select lists; reserve AddToSelection for multi-select","Verify the ListView style (LVS_MULTISELECT) matches the automation script assumptions","Write automation helpers that pick Select vs AddToSelection automatically from the container capability"],"tags":["uiautomation","wpf","listview","selection","single-select"],"backgroundTag":"multiple-selection-not-supported","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"}