dotnet/wpf · error · InvalidOperationException

SR.UIA_OperationCannotBePerformed

Error message

SR.UIA_OperationCannotBePerformed

What it means

After the enabled check, IExpandCollapseProvider.Expand on MenuItemAutomationPeer throws InvalidOperationException(SR.UIA_OperationCannotBePerformed) if the item's role is not TopLevelHeader or SubmenuHeader or if it has no items. Only menu items that actually host a submenu can be expanded; leaf items cannot.

Solutions

  1. Check ExpandCollapseState / whether the MenuItem has children (HasItems, Role) before calling Expand.
  2. Only invoke Expand on items whose role is TopLevelHeader or SubmenuHeader; use Invoke for leaf items.
  3. Verify the menu's ItemsSource actually produces child items at runtime.
  4. Catch InvalidOperationException around Expand in generic pattern-driven automation code.

Example fix

// before
expandCollapsePattern.Expand(); // leaf item -> throws
// after
var role = ((MenuItemAutomationPeer)peer).GetRole();
if (role == MenuItemRole.TopLevelHeader || role == MenuItemRole.SubmenuHeader)
{
    expandCollapsePattern.Expand();
}
Defensive patterns

Strategy: validation

Validate before calling

var role = ((MenuItemAutomationPeer)peer).GetRole(); if (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader) skipExpand();

Type guard

bool HasSubmenu(MenuItem m) => m is { HasItems: true } && m.Role is MenuItemRole.TopLevelHeader or MenuItemRole.SubmenuHeader;

Try / catch

try { expandCollapsePattern.Expand(); } catch (InvalidOperationException) { /* leaf item: use Invoke instead */ }

Prevention

When it happens

Trigger: Calling Expand() on a MenuItem automation peer whose Role is TopLevelItem or SubmenuItem (a leaf item without submenu), or on a header-role item whose HasItems is false.

Common situations: Generic UIA test frameworks that call Expand on every menu element encountered, including leaf items; dynamic menus where items were expected to have children but the ItemsSource yielded an empty collection; automation scripts recorded against a menu layout that later changed.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs:202

                }
            }

            return children;
        }

        ///
        void IExpandCollapseProvider.Expand()
        {
            if(!IsEnabled())
                throw new ElementNotEnabledException();

            MenuItem owner = (MenuItem)Owner;
            MenuItemRole role = owner.Role;

            if (    (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)
                ||  !owner.HasItems)
            {
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
            }

            owner.OpenMenu();
        }

        ///
        void IExpandCollapseProvider.Collapse()
        {
            if(!IsEnabled())
                throw new ElementNotEnabledException();

            MenuItem owner = (MenuItem)Owner;
            MenuItemRole role = owner.Role;

            if (    (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)
                ||  !owner.HasItems)
            {
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);

View on GitHub (pinned to 81131a70a4)