dotnet/wpf · error · InvalidOperationException

SR.SetFocusFailed

Error message

SR.SetFocusFailed

What it means

SetFocus first asks the provider whether the element can receive focus; only when that check returns true does it call UiaCoreApi.UiaSetFocus. When the element reports it cannot receive focus, the library throws InvalidOperationException rather than attempting a doomed OS focus call.

Solutions

  1. Check IsEnabled and whether the control is focusable before calling SetFocus
  2. Use TrySetFocus-style logic: catch InvalidOperationException and fall back to clicking the element
  3. Target the inner focusable child (e.g. the edit box inside a combo) instead of the container

Example fix

// before
element.SetFocus();
// after
if ((bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))
    element.SetFocus();
else
    throw new SkipElementException("Element cannot receive focus");
Defensive patterns

Strategy: try-catch

Validate before calling

static bool CanFocus(AutomationElement e) =>
    (bool)e.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) &&
    !(bool)e.GetCurrentPropertyValue(AutomationElement.IsOffscreenProperty);

Type guard

bool IsFocusable(AutomationElement e) =>
    e.Current.IsEnabled && e.Current.IsKeyboardFocusable;

Try / catch

try { element.SetFocus(); }
catch (InvalidOperationException) { /* element cannot receive focus; use click or skip */ }

Prevention

When it happens

Trigger: Calling AutomationElement.SetFocus() on an element whose provider's CanReceiveFocus (via UiaCoreApi.UiaGetPropertyValue / focus-check) returns false — e.g. focus on a disabled control, a label/static text, or an element whose hwnd cannot take keyboard focus.

Common situations: Test automation scripts that blindly focus controls; trying to focus containers or read-only elements; elements whose underlying window lost its enabled state at runtime.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs:856

            return (AutomationPattern[])interfaces.ToArray(typeof(AutomationPattern));
        }

        /// <summary>
        /// Request to set focus to this element
        /// </summary>
        public void SetFocus()
        {
            CheckElement();

            object canReceiveFocus = GetCurrentPropertyValue(AutomationElement.IsKeyboardFocusableProperty);

            if (canReceiveFocus is bool && (bool)canReceiveFocus)
            {
                UiaCoreApi.UiaSetFocus(_hnode);
            }
            else
            {
                throw new InvalidOperationException(SR.SetFocusFailed);
            }
        }

        /// <summary>
        /// Get a point that can be clicked on.  If there is no ClickablePoint return false
        /// </summary>
        /// <param name="pt">A point that can be used ba a client to click on this LogicalElement</param>
        /// <returns>true if there is point that is clickable</returns>
        public bool TryGetClickablePoint( out Point pt )
        {
            // initialize point here so if we return false its initialized
            pt = new Point (0, 0);

            // Request the provider for a clickable point. 
            object ptClickable = GetCurrentPropertyValue(AutomationElement.ClickablePointProperty);

            if (ptClickable == NotSupported)
            {

View on GitHub (pinned to 81131a70a4)