dotnet/wpf · error · System.InvalidOperationException

SR.DoesNotSupportMultipleSelection

Error message

SR.DoesNotSupportMultipleSelection

What it means

WindowsMenu's ISelectionItemProvider.AddToSelection deliberately throws InvalidOperationException with SR.DoesNotSupportMultipleSelection. Win32 menus only support a single selected item, so multi-select semantics are not available on menu item proxies. The provider implements ISelectionItemProvider for selection reporting, but refuses the multi-select-style add operation.

Solutions

  1. Use SelectionItemPattern.Select() instead of AddToSelection() to select the menu item.
  2. Check the container's SelectionPattern.CanSelectMultiple property before calling AddToSelection; only call it when multiple selection is supported.
  3. If the goal is to open a menu, use InvokePattern.Invoke() or send the underlying keyboard/mouse gesture instead of selection APIs.

Example fix

// before
var selectionItem = menuItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
selectionItem.AddToSelection();

// after
var selectionItem = menuItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
selectionItem.Select(); // menus support single selection only
Defensive patterns

Strategy: validation

Validate before calling

var container = menuItem.FindFirst(TreeScope.Ancestors,
    new PropertyCondition(AutomationElement.IsSelectionPatternAvailableProperty, true));
bool multi = container?.GetCurrentPattern(SelectionPattern.Pattern) is SelectionPattern sp && sp.Current.CanSelectMultiple;
if (!multi) itemPattern.Select(); // never AddToSelection on menus

Type guard

static bool SupportsMultiSelect(AutomationElement el)
{
    return el.FindFirst(TreeScope.Ancestors,
        new PropertyCondition(AutomationElement.IsSelectionPatternAvailableProperty, true))
        ?.GetCurrentPattern(SelectionPattern.Pattern) is SelectionPattern sp && sp.Current.CanSelectMultiple;
}

Try / catch

try { itemPattern.AddToSelection(); }
catch (InvalidOperationException) { itemPattern.Select(); }

Prevention

When it happens

Trigger: Calling UIA's SelectionItemPattern.AddToSelection() on a Win32 menu item proxy (WindowsMenu item) retrieved from a classic Win32 menu (Menu bar, submenu, context menu).

Common situations: Automation scripts written generically against SelectionItemPattern that call AddToSelection on every element supporting the pattern; test frameworks ported from list-view/tree automation to menu automation; list boxes with multi-selection semantics being driven through menu-item code paths.

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/7884b9c1749b1cf3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsMenu.cs:1799

            // Selects this element
            void ISelectionItemProvider.Select()
            {
                // Make sure that the control is enabled
                if (!IsEnabledMenu)
                {
                    throw new ElementNotEnabledException();
                }

                SetFocus();

                // sending enter key to the currently selected item
                ExpandViaEnter();
            }

            // 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 IsChecked() ? true : false;
                }
            }

            // Returns the container for this element

View on GitHub (pinned to 81131a70a4)