{"record":{"id":"77458191cd104d89","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-uia-operationcannotbeperformed-774581","errorCode":null,"errorMessage":"Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed","messagePattern":"Microsoft\\.Windows\\.Controls\\.SR\\.UIA_OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonQuickAccessToolBarAutomationPeer.cs","lineNumber":107,"sourceCode":"                newValue ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed);\n        }\n\n        private RibbonQuickAccessToolBar OwningToolBar\n        {\n            get { return (RibbonQuickAccessToolBar)Owner; }\n        }\n\n        #region IExpandCollapseProvider Members\n\n        public void Collapse()\n        {\n            if (OwningToolBar.HasOverflowItems)\n            {\n                OwningToolBar.IsOverflowOpen = false;\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n        }\n\n        public void Expand()\n        {\n            if (OwningToolBar.HasOverflowItems)\n            {\n                OwningToolBar.IsOverflowOpen = true;\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n        }\n\n        public ExpandCollapseState ExpandCollapseState\n        {\n            get ","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonQuickAccessToolBarAutomationPeer.cs#L89-L125","documentation":"RibbonQuickAccessToolBarAutomationPeer.ExpandCollapseProvider.Collapse() closes the toolbar's overflow only when the toolbar actually has overflow items (OwningToolBar.HasOverflowItems). If there is nothing in the overflow, there is no expandable state, so Collapse() throws InvalidOperationException (SR.UIA_OperationCannotBePerformed).","triggerScenarios":"Calling Collapse() on the quick access toolbar's ExpandCollapse pattern when RibbonQuickAccessToolBar.HasOverflowItems is false — i.e. the overflow popup contains no items.","commonSituations":"UIA test scripts that collapse/expand the QAT regardless of window size or item count; small windows or few QAT items mean no overflow exists.","solutions":["Check OwningToolBar.HasOverflowItems (or read the current ExpandCollapseState) before calling Collapse()","Ensure the QAT has more items than fit so an overflow exists, if the test expects an overflow to collapse","Catch InvalidOperationException and treat it as 'nothing to collapse'","Query ExpandCollapseState property first; only invoke Collapse when it is Expanded"],"exampleFix":"// before\n((ExpandCollapsePattern)qatAutomationPeer.GetPattern(ExpandCollapsePattern.Pattern)).Collapse();\n// after\nvar provider = (IExpandCollapseProvider)qatAutomationPeer.GetPattern(ExpandCollapsePattern.Pattern);\nif (provider.ExpandCollapseState == ExpandCollapseState.Expanded) provider.Collapse();","handlingStrategy":"validation","validationCode":"bool canCollapse = qatPeer.GetPattern(ExpandCollapsePattern.Pattern) is ExpandCollapsePattern p\n    && p.ExpandCollapseState == ExpandCollapseState.Expanded;\nif (!canCollapse) return;","typeGuard":"bool HasOverflow(RibbonQuickAccessToolBar qat) => qat.HasOverflowItems;\n","tryCatchPattern":"try { provider.Collapse(); }\ncatch (InvalidOperationException) { /* no overflow items: nothing to collapse */ }","preventionTips":["Read ExpandCollapseState before Expand/Collapse calls","Check HasOverflowItems when testing the quick access toolbar","Size the test window (or add QAT items) so an overflow exists when the scenario requires one"],"tags":["uia","wpf","ribbon","expandcollapse","invalid-state"],"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-22T01:17:13.364Z"}