dotnet/wpf · error · InvalidOperationException
SR.DoesNotSupportMultipleSelection
Error message
SR.DoesNotSupportMultipleSelection
What it means
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.
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.
Example fix
// before
selItem.AddToSelection(); // throws on single-select lists
// after
var sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);
if (sel.Current.CanSelectMultiple)
selItem.AddToSelection();
else
selItem.Select(); Defensive patterns
Strategy: validation
Validate before calling
var sel = (SelectionPattern)list.GetCurrentPattern(SelectionPattern.Pattern);
if (!sel.Current.CanSelectMultiple && (sel.Current.GetSelection()?.Length ?? 0) > 0)
throw new InvalidOperationException("Single-select list already has a selection; use Select() instead of AddToSelection()."); Type guard
bool SupportsMultiSelect(AutomationElement list) =>
list.TryGetCurrentPattern(SelectionPattern.Pattern, out var p) && ((SelectionPattern)p).Current.CanSelectMultiple; Try / catch
try { selItem.AddToSelection(); }
catch (InvalidOperationException) { selItem.Select(); } // fall back to replace-selection on single-select lists Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- SR.SelectionRequired
- SR.OperationCannotBePerformed
- ElementNotEnabledException
- SR.DataGridRow_CannotSelectRowWhenCells
- SR.OperationCannotBePerformed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/beddd31ce108f76d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItem.cs:430
}
// simple case: object already selected
if (WindowsListView.IsItemSelected (_hwnd, _item))
{
return;
}
// object does not support multi-selection
if (!WindowsListView.MultiSelected(_hwnd))
{
IRawElementProviderSimple container = ((ISelectionItemProvider)this).SelectionContainer;
bool selectionRequired = container != null ? ((ISelectionProvider)container).IsSelectionRequired : true;
// For single selection containers that IsSelectionRequired == false and nothing is selected
// an AddToSelection is valid.
if (selectionRequired || WindowsListView.GetSelectedItemCount(_hwnd) > 0)
{
throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);
}
}
// At this point we know: Item either supports multiple selection or nothing
// is selected in the list
// Try to select an item
if (!WindowsListView.SelectItem(_hwnd, _item))
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);
}
}
// Removes this element from the selection
void ISelectionItemProvider.RemoveFromSelection ()
{
// Make sure that the control is enabled
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{View on GitHub (pinned to 81131a70a4)