{"record":{"id":"d14c4dd15a3ae7cf","repo":"dotnet/wpf","slug":"uia-operationcannotbeperformed","errorCode":null,"errorMessage":"UIA_OperationCannotBePerformed","messagePattern":"UIA_OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuButtonAutomationPeer.cs","lineNumber":172,"sourceCode":"                if ((OwningMenuButton.CanUserResizeHorizontally || OwningMenuButton.CanUserResizeVertically) \n                    && OwningMenuButton.IsDropDownOpen)\n                    return this;\n            }\n\n            return base.GetPattern(patternInterface);\n        }\n\n        #region IExpandCollapseProvider Members\n\n        void IExpandCollapseProvider.Collapse()\n        {\n            if (!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            RibbonMenuButton owner = OwningMenuButton;\n            if (!owner.HasItems)\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n\n            owner.IsDropDownOpen = false;\n        }\n\n        void IExpandCollapseProvider.Expand()\n        {\n            if (!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            RibbonMenuButton owner = OwningMenuButton;\n            if (!owner.HasItems)\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n\n            owner.IsDropDownOpen = true;\n        }","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuButtonAutomationPeer.cs#L154-L190","documentation":"RibbonMenuButtonAutomationPeer.Collapse (the IExpandCollapseProvider implementation) throws InvalidOperationException(SR.UIA_OperationCannotBePerformed) when the owning RibbonMenuButton has no items. Collapsing a dropdown that contains nothing is rejected as an unsupported UIA operation. An ElementNotEnabledException is thrown earlier if the control is disabled.","triggerScenarios":"UIA client calling ExpandCollapsePattern.Collapse on a RibbonMenuButton whose Items/HasItems is empty — e.g. all items were removed, data binding produced no entries, or the button is a placeholder before items load.","commonSituations":"Test suites that collapse every discovered menu button indiscriminately, hitting item-less buttons; apps whose Ribbon menu content is populated asynchronously or per user role and starts empty; telemetry/accessibility tools traversing the Ribbon tree.","solutions":["Before Collapse, check owner.HasItems (or that the automation peer's children exist) and skip the call when empty.","Guard the pattern availability: only collapse when ExpandCollapseState is Expanded and the button has items.","Bind/verify that the RibbonMenuButton's ItemsSource is populated before exposing the control to UIA scenarios.","Catch InvalidOperationException around Collapse and treat empty menus as a no-op rather than a test failure."],"exampleFix":"// before\nexpandCollapsePattern.Collapse(); // throws when no items\n// after\nvar menuButton = (RibbonMenuButton)targetControl;\nif (menuButton.HasItems && menuButton.IsDropDownOpen)\n    expandCollapsePattern.Collapse();","handlingStrategy":"validation","validationCode":"var menuButton = (RibbonMenuButton)targetControl;\nbool canCollapse = menuButton.HasItems && menuButton.IsDropDownOpen;\n","typeGuard":"bool CanCollapse(RibbonMenuButton b) => b != null && b.HasItems;","tryCatchPattern":"try { expandCollapsePattern.Collapse(); }\ncatch (InvalidOperationException)\n{\n    // empty or already-closed menu — treat as no-op\n}","preventionTips":["Check HasItems before Expand/Collapse calls on ribbon menus.","Only collapse when ExpandCollapseState reports Expanded.","Ensure ItemsSource is populated (await async loads) before UIA expand/collapse steps.","Filter empty menu buttons out of UIA traversal-based test loops."],"tags":["wpf","ribbon","ui-automation","expand-collapse"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}