{"record":{"id":"97b1c7402dc7f94e","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-uia-operationcannotbeperformed","errorCode":null,"errorMessage":"Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed","messagePattern":"Microsoft\\.Windows\\.Controls\\.SR\\.UIA_OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs","lineNumber":151,"sourceCode":"        {\n            if (!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            UIElement owner = GetWrapper();\n            if (owner == null)\n            {\n                throw new ElementNotAvailableException(Microsoft.Windows.Controls.SR.VirtualizedElement);\n            }\n\n            RibbonMenuItem menuItemOwner = owner as RibbonMenuItem;\n            if (menuItemOwner != null)\n            {\n                MenuItemRole role = menuItemOwner.Role;\n\n                if ((role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)\n                    || !menuItemOwner.HasItems)\n                {\n                    throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n                }\n\n                menuItemOwner.IsSubmenuOpen = true;\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n        }\n\n        ///\n        void IExpandCollapseProvider.Collapse()\n        {\n            if (!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            UIElement owner = GetWrapper();\n            if (owner == null)","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs#L133-L169","documentation":"Inside IExpandCollapseProvider.Expand, the peer has realized the RibbonMenuItem but verifies the item can actually expand: its Role must be TopLevelHeader or SubmenuHeader and it must have items. A leaf menu item (or an empty header) cannot expand, so the peer throws InvalidOperationException with SR.UIA_OperationCannotBePerformed.","triggerScenarios":"Calling Expand() on a RibbonMenuItem whose MenuItemRole is TopLevelItem or SubmenuItem (no submenu), or on a header item whose HasItems is false.","commonSituations":"Test suites iterating all ribbon menu items and calling Expand on each; applications where ribbon categories contain only leaf items so headers have no children.","solutions":["Only call Expand on menu items that act as headers with children; check the item's role/items first via the RibbonMenuItem API or the automation tree.","Read ExpandCollapseState instead of calling Expand for leaf items - they are always Collapsed and not expandable.","Catch InvalidOperationException around Expand and treat it as 'item has no submenu'.","Restructure ribbon data so header items always contain items if expandability is expected."],"exampleFix":"// before\nexpandCollapsePattern.Expand();\n// after\nif (menuItem.HasItems && (menuItem.Role == MenuItemRole.TopLevelHeader || menuItem.Role == MenuItemRole.SubmenuHeader))\n    expandCollapsePattern.Expand();","handlingStrategy":"validation","validationCode":"bool canExpand = menuItem.HasItems && (menuItem.Role == MenuItemRole.TopLevelHeader || menuItem.Role == MenuItemRole.SubmenuHeader);","typeGuard":"var header = owner as RibbonMenuItem; if (header != null && header.HasItems && header.HasItems) { /* expandable */ }","tryCatchPattern":null,"preventionTips":["Check Role and HasItems before Expand","Read ExpandCollapseState for leaf items instead of expanding them","Skip non-header items in automation sweeps"],"tags":["wpf","uia","automation","menu"],"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"}