dotnet/wpf · error · InvalidOperationException

Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed

Error message

Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed

What it means

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).

Solutions

  1. Check OwningToolBar.HasOverflowItems (or read the current ExpandCollapseState) before calling Collapse()
  2. Ensure the QAT has more items than fit so an overflow exists, if the test expects an overflow to collapse
  3. Catch InvalidOperationException and treat it as 'nothing to collapse'
  4. Query ExpandCollapseState property first; only invoke Collapse when it is Expanded

Example fix

// before
((ExpandCollapsePattern)qatAutomationPeer.GetPattern(ExpandCollapsePattern.Pattern)).Collapse();
// after
var provider = (IExpandCollapseProvider)qatAutomationPeer.GetPattern(ExpandCollapsePattern.Pattern);
if (provider.ExpandCollapseState == ExpandCollapseState.Expanded) provider.Collapse();
Defensive patterns

Strategy: validation

Validate before calling

bool canCollapse = qatPeer.GetPattern(ExpandCollapsePattern.Pattern) is ExpandCollapsePattern p
    && p.ExpandCollapseState == ExpandCollapseState.Expanded;
if (!canCollapse) return;

Type guard

bool HasOverflow(RibbonQuickAccessToolBar qat) => qat.HasOverflowItems;

Try / catch

try { provider.Collapse(); }
catch (InvalidOperationException) { /* no overflow items: nothing to collapse */ }

Prevention

When it happens

Trigger: Calling Collapse() on the quick access toolbar's ExpandCollapse pattern when RibbonQuickAccessToolBar.HasOverflowItems is false — i.e. the overflow popup contains no items.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/77458191cd104d89. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonQuickAccessToolBarAutomationPeer.cs:107

                newValue ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed);
        }

        private RibbonQuickAccessToolBar OwningToolBar
        {
            get { return (RibbonQuickAccessToolBar)Owner; }
        }

        #region IExpandCollapseProvider Members

        public void Collapse()
        {
            if (OwningToolBar.HasOverflowItems)
            {
                OwningToolBar.IsOverflowOpen = false;
            }
            else
            {
                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);
            }
        }

        public void Expand()
        {
            if (OwningToolBar.HasOverflowItems)
            {
                OwningToolBar.IsOverflowOpen = true;
            }
            else
            {
                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);
            }
        }

        public ExpandCollapseState ExpandCollapseState
        {
            get 

View on GitHub (pinned to 81131a70a4)