dotnet/wpf · error · InvalidOperationException

SR.DoesNotSupportMultipleSelection

Error message

SR.DoesNotSupportMultipleSelection

What it means

AddToSelection throws InvalidOperationException(SR.DoesNotSupportMultipleSelection) when the item's container requires a selection (IsSelectionRequired true) and already has one, or is otherwise a single-selection listbox — so adding another selected item would effectively create multiple selection, which the control does not support.

Solutions

  1. Use Select instead of AddToSelection to replace the existing selection.
  2. Verify SelectionPattern.CanSelectMultiple is true before calling AddToSelection.
  3. If multi-select is needed, use a listbox with LBS_EXTENDEDSEL/LBS_MULTIPLESEL.

Example fix

// before
selectionItem.AddToSelection();
// after
if (!(listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern).Current.CanSelectMultiple)
{
    selectionItem.Select(); // replace selection instead
}
else
{
    selectionItem.AddToSelection();
}
Defensive patterns

Strategy: validation

Validate before calling

bool canAdd = listbox.GetCurrentPattern(SelectionPattern.Pattern) is SelectionPattern sp && sp.Current.CanSelectMultiple;
if (!canAdd) { selectionItem.Select(); }

Type guard

bool SupportsMultiSelect(AutomationElement listbox) => (listbox.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern)?.Current.CanSelectMultiple == true;

Try / catch

try { selectionItem.AddToSelection(); }
catch (InvalidOperationException) { selectionItem.Select(); // fallback: replace selection }

Prevention

When it happens

Trigger: Calling AddToSelection on an item of a single-selection listbox (IsMultipleSelection false) where a selection is required or a selection already exists and the item is not already selected.

Common situations: Treating a standard single-select ListBox as multi-select; assuming UIA AddToSelection behaves like click-with-ctrl on any list; migrating automation from HTML select multiple to Win32 listbox.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/97fb68faf9563512. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListBox.cs:842

                if (ListboxItem.IsSelected (_hwnd, _item) && !_listBox.IsParentedByCombo())
                {
                    // if it is a combo, then always perform the selection otherwise the listbox won't disappear
                    return;
                }

                bool multipleSelection = _listBox.IsMultipleSelection();

                // object does not support multi-selection
                if (!multipleSelection)
                {
                    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 || _listBox.HasSelection())
                    {
                        throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);
                    }
                }

                if (_listBox.IsParentedByCombo())
                {
                    // if this is a combo and the listbox is not displayed the selection will not stick, so 
                    // display the listbox before doing the select.
                    if (((IExpandCollapseProvider)_listBox._parent).ExpandCollapseState == ExpandCollapseState.Collapsed)
                    {
                        ((IExpandCollapseProvider)_listBox._parent).Expand();
                    }
                }

                // At this point we know: Item either supports multiple selection or nothing
                // is selected in the list
                // Try to select an item
                if (!Select(multipleSelection))
                {

View on GitHub (pinned to 81131a70a4)