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
- Set IsFocusable/IsEnabled/IsVisible on the owner element before calling SetFocus
- Use ownerElement.Focus() directly and check its bool return instead of throwing
- Call FocusManager.Focus() and verify the focused element afterwards
- 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
- Ensure owner elements have Focusable=true before automation
- Only call SetFocus on visible, enabled, loaded elements
- Prefer direct control.Focus() and check the bool return in app code
- In UIA clients, treat focus failure as non-fatal
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
- NotSupportedException
- SetFocusFailed
- SR.SetFocusFailed
- ArgumentOutOfRangeException
- ElementNotAvailableException
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)