dotnet/wpf · error · InvalidOperationException

SetFocusFailed

Error message

SetFocusFailed

What it means

RibbonComboBoxAutomationPeer.SetFocusCore throws InvalidOperationException(SR.SetFocusFailed) when SetFocus on the RibbonComboBox did not result in keyboard focus landing where expected. For an EDITABLE combo box, this specific throw fires when the internal editable TextBox (EditableTextBoxSite) is null or does not have keyboard focus after the focus attempt. UI Automation clients see 'operation failed' when automating focus.

Solutions

  1. Ensure the Ribbon tab/group containing the ComboBox is selected and visible before calling SetFocus via automation.
  2. For editable combos, drive focus to owner.EditableTextBoxSite directly (tb.Focus()) and verify IsKeyboardFocused instead of relying on peer.SetFocus.
  3. Retry SetFocus after a short dispatcher delay to let layout/popups settle, then fall back to selecting the combo item programmatically.
  4. Catch InvalidOperationException from SetFocus in the automation driver and log it as 'control not focusable in current visual state' rather than failing the whole scenario.

Example fix

// before
var peer = (RibbonComboBoxAutomationPeer)comboBox.CreateAutomationPeer();
peer.SetFocus(); // throws SetFocusFailed if textbox not focused
// after
var peer = (RibbonComboBoxAutomationPeer)comboBox.CreateAutomationPeer();
var combo = (RibbonComboBox)peer.Owner;
if (combo.IsVisible && combo.IsEnabled)
{
    var tb = combo.EditableTextBoxSite;
    if (tb != null) tb.Focus();
    else combo.Focus();
}
Defensive patterns

Strategy: validation

Validate before calling

var combo = (RibbonComboBox)peer.Owner;
if (!combo.IsVisible || !combo.IsEnabled) return;
if (combo.IsEditable && (combo.EditableTextBoxSite == null || !combo.EditableTextBoxSite.IsKeyboardFocused))
{
    combo.EditableTextBoxSite?.Focus();
}

Try / catch

try { peer.SetFocus(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("focus", StringComparison.OrdinalIgnoreCase))
{
    // focus went elsewhere; fall back to direct focus on the combo/textbox
    owner.Focus();
}

Prevention

When it happens

Trigger: IUIAutomation invoking SetFocus (or AutomationPeer.SetFocus) on an editable RibbonComboBox whose EditableTextBoxSite is null, or whose TextBox did not receive IsKeyboardFocused after the focus call — e.g. the combo is not visible/enabled, another element stole focus, or the combo is inside a collapsed Ribbon group.

Common situations: UIA test suites (Coded UI, FlaUI, WinAppDriver) clicking/selecting a RibbonComboBox while the Ribbon tab hosting it is not active; editable combo whose TextBox part hasn't been realized yet; focus moved asynchronously to a popup before the check runs.

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/28ca82f54aa55de6. Report an issue: GitHub.

Appendix: source

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

                }
            }

            return children;
        }

        protected override void SetFocusCore()
        {
            RibbonComboBox owner = (RibbonComboBox)Owner;
            if (owner.Focusable)
            {
                if (!owner.Focus())
                {
                    //The focus might have gone to the TextBox inside Combobox if it is editable.
                    if (owner.IsEditable)
                    {
                        TextBox tb = owner.EditableTextBoxSite;
                        if (tb == null || !tb.IsKeyboardFocused)
                            throw new InvalidOperationException(Microsoft.Windows.Controls.SR.SetFocusFailed);
                    }
                    else
                        throw new InvalidOperationException(Microsoft.Windows.Controls.SR.SetFocusFailed);
                }
            }
            else
            {
                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.SetFocusFailed);
            }
        }

        #endregion
        
        #region IValueProvider Members

        public bool IsReadOnly
        {
            get

View on GitHub (pinned to 81131a70a4)