dotnet/wpf · error · System.InvalidOperationException

SR.OperationCannotBePerformed

Error message

SR.OperationCannotBePerformed

What it means

WindowsMenu's IExpandCollapseProvider.Expand throws InvalidOperationException(SR.OperationCannotBePerformed) when the item is a system menu item but ExpandCollapseSystem(true) fails to expand it. The provider cannot expand the system menu via the attempted Win32 mechanism, so per UIA provider guidelines it throws an InvalidOperationException rather than silently failing.

Solutions

  1. Verify the target window is a normal top-level window whose system menu can be programmatically opened before expanding
  2. Catch InvalidOperationException and fall back to alternative automation (e.g. Alt+Space keystrokes) to open the system menu
  3. Retry the expand after bringing the window to the foreground (SetFocus) since system-menu operations often require focus
  4. Skip system menu items if UIA automation of system menus is not required for the scenario

Example fix

// before
((ExpandCollapsePattern)sysItem.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();
// after
try
{
    ((ExpandCollapsePattern)sysItem.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();
}
catch (InvalidOperationException)
{
    // fallback: send Alt+Space to open the system menu
    Keyboard.SendKeys(targetWindow, "% ");
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (itemName.StartsWith("System menu") && !IsWindowForeground(targetWindow))
    SetForegroundWindow(targetWindow); // system menu ops need focus

Type guard

null

Try / catch

try { expandPattern.Expand(); }
catch (InvalidOperationException)
{ /* fall back to Alt+Space keyboard automation */ }

Prevention

When it happens

Trigger: Calling Expand() on a menu item identified as a system menu item (IsSystemMenuItem true) where the ExpandCollapseSystem(true) Win32 call returns false (e.g. the system menu could not be posted/toggled).

Common situations: Automating title-bar/system menus of windows that are in a state where the system menu cannot be opened (wrong window handle, unusual window styles, elevated vs non-elevated process mismatches).

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

Appendix: source

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

                    throw new ElementNotEnabledException();
                }

                if (!IsSubmenuCollapsed ())
                {
                    return;
                }

                // For some menus we need to have a right to input
                if (ReadyForInput ())
                {
                    // sytem menu item has its own way to expand
                    // this should preceed all other expand calls
                    if (IsSystemMenuItem())
                    {
                        if (ExpandCollapseSystem(true))
                            return;

                        throw new InvalidOperationException(SR.OperationCannotBePerformed);
                    }

                    // top level
                    if (_menuType == WindowsMenu.MenuType.Toplevel)
                    {
                        if (ExpandTopLevelMenu())
                            return;

                        throw new InvalidOperationException(SR.OperationCannotBePerformed);
                    }

                    // submenu
                    if( ExpandSubmenu ())
                        return;
                }

                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

View on GitHub (pinned to 81131a70a4)