{"record":{"id":"a3213fdc5f102ccf","repo":"dotnet/wpf","slug":"sr-uia-operationcannotbeperformed-menuitemautomationpeer","errorCode":null,"errorMessage":"SR.UIA_OperationCannotBePerformed","messagePattern":"SR\\.UIA_OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs","lineNumber":202,"sourceCode":"                }\n            }\n\n            return children;\n        }\n\n        ///\n        void IExpandCollapseProvider.Expand()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            MenuItem owner = (MenuItem)Owner;\n            MenuItemRole role = owner.Role;\n\n            if (    (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)\n                ||  !owner.HasItems)\n            {\n                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);\n            }\n\n            owner.OpenMenu();\n        }\n\n        ///\n        void IExpandCollapseProvider.Collapse()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            MenuItem owner = (MenuItem)Owner;\n            MenuItemRole role = owner.Role;\n\n            if (    (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)\n                ||  !owner.HasItems)\n            {\n                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs#L184-L220","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check ExpandCollapseState / whether the MenuItem has children (HasItems, Role) before calling Expand.","Only invoke Expand on items whose role is TopLevelHeader or SubmenuHeader; use Invoke for leaf items.","Verify the menu's ItemsSource actually produces child items at runtime.","Catch InvalidOperationException around Expand in generic pattern-driven automation code."],"exampleFix":"// before\nexpandCollapsePattern.Expand(); // leaf item -> throws\n// after\nvar role = ((MenuItemAutomationPeer)peer).GetRole();\nif (role == MenuItemRole.TopLevelHeader || role == MenuItemRole.SubmenuHeader)\n{\n    expandCollapsePattern.Expand();\n}","handlingStrategy":"validation","validationCode":"var role = ((MenuItemAutomationPeer)peer).GetRole(); if (role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader) skipExpand();","typeGuard":"bool HasSubmenu(MenuItem m) => m is { HasItems: true } && m.Role is MenuItemRole.TopLevelHeader or MenuItemRole.SubmenuHeader;","tryCatchPattern":"try { expandCollapsePattern.Expand(); } catch (InvalidOperationException) { /* leaf item: use Invoke instead */ }","preventionTips":["Only call Expand on header-role menu items; use Invoke on leaf items.","Verify the menu item actually has child items (HasItems).","Update recorded UIA scripts when menu layouts change.","Catch InvalidOperationException in generic Expand-driven automation."],"tags":["wpf","ui-automation","menuitem","unsupported-operation"],"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"}