dotnet/wpf · error · InvalidOperationException

SR.SetFocusFailed

Error message

SR.SetFocusFailed

What it means

ComboBoxAutomationPeer.SetFocusCore verifies after attempting to set focus that keyboard focus actually landed where expected. For an editable ComboBox, if the editable TextBox (EditableTextBoxSite) is null or did not receive keyboard focus, the peer throws InvalidOperationException(SR.SetFocusFailed) — the post-condition check of the UIA SetFocus contract.

Solutions

  1. Ensure the ComboBox is visible, loaded, and its template applied before calling SetFocus.
  2. Focus the ComboBox control itself (Keyboard.Focus) and verify IsKeyboardFocused before automating.
  3. Retry SetFocus after layout settles (Dispatcher.BeginInvoke at Loaded priority).
  4. Catch InvalidOperationException in the automation client and retry, or select the item programmatically instead.

Example fix

// before
comboPeer.SetFocus(); // may fail before template is realized
// after
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => comboPeer.SetFocus()));
Defensive patterns

Strategy: try-catch

Validate before calling

if (!combo.IsLoaded || !combo.IsVisible || !combo.IsEditable)
    throw new InvalidOperationException("Editable ComboBox not ready for SetFocus.");

Try / catch

try { comboPeer.SetFocus(); }
catch (InvalidOperationException)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => comboPeer.SetFocus()));
}

Prevention

When it happens

Trigger: Calling SetFocus() on an editable ComboBox's automation peer while the inner TextBox cannot take focus — not yet realized (template not applied), ComboBox closed or mid-animation, or focus intercepted by another element.

Common situations: UIA tests focusing a ComboBox inside a not-yet-loaded template; editable combo in a virtualized or collapsed container; another control grabbing focus immediately after the SetFocus attempt.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ComboBoxAutomationPeer.cs:99

            }

            return children;
        }

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

        ///
        internal void ScrollItemIntoView(object item)
        {
            // Item can be scrolled into view only when the Combo is expanded.
            // If combo is not expanded the possible solution is expanding scrolling and 
            // bringing back to original state but that is a breaking change and if there is a strong case can be supported later. 
            if(((IExpandCollapseProvider)this).ExpandCollapseState == ExpandCollapseState.Expanded)

View on GitHub (pinned to 81131a70a4)