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

  1. Check the container's SelectionPattern.CanSelectMultiple property before calling AddToSelection; use Select() (replace selection) for single-select lists.
  2. If multi-select behavior is needed, fix the ListView to include LVS_MULTISELECT in the app's control style.
  3. Simulate multi-select via keyboard input (SendKeys Ctrl+Click) instead of the SelectionItem pattern on single-select lists.
  4. 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

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


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)