{"record":{"id":"25fcfc501c887a09","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed-windowsmenu","errorCode":null,"errorMessage":"SR.OperationCannotBePerformed","messagePattern":"SR\\.OperationCannotBePerformed","errorType":"exception","errorClass":"System.InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsMenu.cs","lineNumber":1704,"sourceCode":"                    throw new ElementNotEnabledException();\n                }\n\n                if (!IsSubmenuCollapsed ())\n                {\n                    return;\n                }\n\n                // For some menus we need to have a right to input\n                if (ReadyForInput ())\n                {\n                    // sytem menu item has its own way to expand\n                    // this should preceed all other expand calls\n                    if (IsSystemMenuItem())\n                    {\n                        if (ExpandCollapseSystem(true))\n                            return;\n\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                    }\n\n                    // top level\n                    if (_menuType == WindowsMenu.MenuType.Toplevel)\n                    {\n                        if (ExpandTopLevelMenu())\n                            return;\n\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                    }\n\n                    // submenu\n                    if( ExpandSubmenu ())\n                        return;\n                }\n\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }","sourceCodeStart":1686,"sourceCodeEnd":1722,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsMenu.cs#L1686-L1722","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Verify the target window is a normal top-level window whose system menu can be programmatically opened before expanding","Catch InvalidOperationException and fall back to alternative automation (e.g. Alt+Space keystrokes) to open the system menu","Retry the expand after bringing the window to the foreground (SetFocus) since system-menu operations often require focus","Skip system menu items if UIA automation of system menus is not required for the scenario"],"exampleFix":"// before\n((ExpandCollapsePattern)sysItem.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();\n// after\ntry\n{\n    ((ExpandCollapsePattern)sysItem.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();\n}\ncatch (InvalidOperationException)\n{\n    // fallback: send Alt+Space to open the system menu\n    Keyboard.SendKeys(targetWindow, \"% \");\n}","handlingStrategy":"try-catch","validationCode":"if (itemName.StartsWith(\"System menu\") && !IsWindowForeground(targetWindow))\n    SetForegroundWindow(targetWindow); // system menu ops need focus","typeGuard":"null","tryCatchPattern":"try { expandPattern.Expand(); }\ncatch (InvalidOperationException)\n{ /* fall back to Alt+Space keyboard automation */ }","preventionTips":["Foreground the window before system menu automation","Match integrity levels between automation client and target (UIPI)","Prefer keyboard fallbacks for system menu manipulation","Skip system menu items when not essential to the scenario"],"tags":["ui-automation","menu","operation-failed"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}