dotnet/wpf · error · InvalidOperationException

SR.SetFocusFailed

Error message

SR.SetFocusFailed

What it means

ContentElementAutomationPeer.SetFocusCore attempts to move keyboard focus to the peer's owner element by calling _owner.Focus(). If the element cannot take focus (e.g. it is not focusable, not visible, or not enabled), WPF throws InvalidOperationException with message SR.SetFocusFailed. This is thrown when UI Automation (or user code) calls SetFocus on the automation peer.

Solutions

  1. Set IsFocusable/IsEnabled/IsVisible on the owner element before calling SetFocus
  2. Use ownerElement.Focus() directly and check its bool return instead of throwing
  3. Call FocusManager.Focus() and verify the focused element afterwards
  4. Wrap SetFocus in try-catch for InvalidOperationException and degrade gracefully in UIA clients

Example fix

// before
contentElementAutomationPeer.SetFocus();
// after
if (contentElement is ContentElement ce && ce.Focusable)
{
    ce.Focus();
}
else
{
    // handle non-focusable element
}
Defensive patterns

Strategy: try-catch

Validate before calling

bool canFocus = contentElement is ContentElement ce && ce.Focusable && ce.IsVisible && ce.IsEnabled;

Type guard

static bool CanSetFocus(ContentElement e) => e is { Focusable: true, IsVisible: true, IsEnabled: true };

Try / catch

try
{
    peer.SetFocus();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("focus"))
{
    // fall back: use FocusManager or select the element instead
}

Prevention

When it happens

Trigger: Calling AutomationPeer.SetFocus() (or an UIA client invoking the Focus pattern) on a ContentElement whose owner cannot acquire keyboard focus — typically because UIElement.Focusable is false, the element is not visible/enabled, or it is not in a focusable parent scope.

Common situations: UI Automation tests or screen readers trying to focus a TextBlock/non-focusable hyperlink; automation running against hidden or disabled controls; app code setting focus through an automation peer during window load before the element is rendered.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/ContentElementAutomationPeer.cs:307

        {
            return AutomationProperties.GetHeadingLevel(_owner);
        }

        /// <summary>
        /// <see cref="AutomationPeer.GetClickablePointCore"/>
        /// </summary>
        protected override Point GetClickablePointCore()
        {
            return new Point(double.NaN, double.NaN);
        }

        /// <summary>
        /// <see cref="AutomationPeer.SetFocusCore"/>
        /// </summary>
        protected override void SetFocusCore()
        {
            if (!_owner.Focus())
                throw new InvalidOperationException(SR.SetFocusFailed);
        }

        ///
        internal override Rect GetVisibleBoundingRectCore()
        {
            return GetBoundingRectangle();
        }
        

        private ContentElement _owner;
        private SynchronizedInputAdaptor _synchronizedInputPattern;
    }
}

View on GitHub (pinned to 81131a70a4)