dotnet/wpf · error · InvalidOperationException

Selection container does not support multiple selection.

Error message

Selection container does not support multiple selection.

What it means

ISelectionItemProvider.AddToSelection on the WindowsButton proxy unconditionally throws InvalidOperationException('Selection container does not support multiple selection.'). A single button element is not part of a multi-select container, so adding it to a selection is meaningless; the proxy models this by throwing SR.DoesNotSupportMultipleSelection. The same throw exists in RemoveFromSelection.

Solutions

  1. Do not call AddToSelection on buttons; instead call the InvokePattern Invoke() method (the proxy routes selection semantics to Invoke).
  2. Check the element's SelectionContainer to confirm it actually supports multiple selection before calling AddToSelection.
  3. Catch InvalidOperationException around AddToSelection/RemoveFromSelection and fall back to Invoke.
  4. If toggle state is what you need, use TogglePattern where the control exposes it instead of SelectionItemPattern.

Example fix

// before
var selItem = button.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
selItem.AddToSelection();
// after
var invoke = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
if (invoke != null) invoke.Invoke();
Defensive patterns

Strategy: validation

Validate before calling

// C# (UIA client)
var selItem = element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
var container = selItem?.Current.SelectionContainer;
bool multiSelect = container != null &&
    (container.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern)?.Current.CanSelectMultiple == true;
// only call AddToSelection when multiSelect is true; otherwise use InvokePattern

Try / catch

try { selItem.AddToSelection(); }
catch (InvalidOperationException)
{
    // unsupported; fall back to InvokePattern
    ((InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
}

Prevention

When it happens

Trigger: A UIA client obtains SelectionItemPattern from a WindowsButton element and calls AddToSelection() on the pattern.

Common situations: Automation scripts generically calling AddToSelection on any SelectionItemPattern element; test frameworks that blanket-exercise every supported pattern on controls; ported code that assumed a list-box-like container.

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/49a1523741e3ded6. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsButton.cs:366

            {
                return false;
            }
        }

        #endregion Selection Pattern

        #region SelectionItem Pattern

        // Selects this element
        void ISelectionItemProvider.Select()
        {
            Invoke();
        }

        // Adds this element to the selection
        void ISelectionItemProvider.AddToSelection()
        {
            throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);
        }

        // Removes this element from the selection
        void ISelectionItemProvider.RemoveFromSelection()
        {
            throw new InvalidOperationException(SR.DoesNotSupportMultipleSelection);
        }

        // True if this element is part of the the selection
        bool ISelectionItemProvider.IsSelected
        {
            get
            {
                return ToggleState == ToggleState.On ? true : false;
            }
        }

        // Returns the container for this element

View on GitHub (pinned to 81131a70a4)