dotnet/wpf · error · InvalidOperationException
Target element cannot receive focus.
Error message
Target element cannot receive focus.
What it means
ProxySimple.SetFocus (IRawElementProviderFragment) first tries the virtual SetFocus() override; if the proxy does not override it (or it cannot focus via HWND), the method throws InvalidOperationException with SR.SetFocusFailed — 'Target element cannot receive focus.' It means this proxy element type has no implemented focus mechanism.
Solutions
- Only call SetFocus on elements whose IsKeyboardFocusable property is true.
- Interact with the element through its supported patterns (Invoke, SelectionItem, Toggle) instead of focusing it.
- Focus a focusable ancestor/child (e.g. the owning window or an inner edit control) and then operate on the element.
Example fix
// before
labelProxy.SetFocus(); // InvalidOperationException: Target element cannot receive focus.
// after
var ae = (AutomationElement)labelProxy;
if (ae.Current.IsKeyboardFocusable)
{
ae.SetFocus();
}
else
{
// operate via patterns instead of focus
var invoke = ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invoke?.Invoke();
} Defensive patterns
Strategy: validation
Validate before calling
static void EnsureFocusable(AutomationElement e)
{
if (!e.Current.IsKeyboardFocusable)
throw new InvalidOperationException($"{e.Current.AutomationId} is not keyboard focusable");
} Type guard
static bool SupportsFocus(AutomationElement e) =>
e.Current.IsKeyboardFocusable &&
(e.Current.ControlType == ControlType.Button ||
e.Current.ControlType == ControlType.Edit ||
e.Current.ControlType == ControlType.Document ||
e.Current.ControlType == ControlType.ListItem); Try / catch
try { element.SetFocus(); }
catch (InvalidOperationException)
{
// element cannot receive focus; drive it via patterns or focus a parent
parentWindow.SetFocus();
} Prevention
- Check IsKeyboardFocusable before calling SetFocus.
- Prefer pattern-based interaction (Invoke/Select/Toggle) over focus manipulation.
- Focus only controls with an HWND that accepts keyboard input (edit, button, list item).
- Treat static text/pane/container elements as non-focusable in automation code.
When it happens
Trigger: Calling SetFocus() on a UI Automation proxy element whose concrete proxy class does not override the virtual SetFocus() and is not keyboard-focusable (no HWND focus target), e.g. a static text or container proxy.
Common situations: Attempting to focus non-focusable controls (labels, panes) in legacy Win32/WinForms automation; using proxies for controls that don't expose WS_TABSTOP or a focusable HWND.
Related errors
- Element is not enabled.
- SR.DoesNotSupportMultipleSelection
- SR.DoesNotSupportMultipleSelection
- SR.Format(SR.RichEditTextPatternHasNoChildren…
- SR.OperationCannotBePerformed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f606945d19f5a953.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/ProxySimple.cs:552
// a SetFocus() was called on them, the case to bool will cause a NullReferenceException.
// So make sure the return is of type bool before casting.
bool isKeyboardFocusable = true;
object isKeyboardFocusableProperty = GetElementProperty(AutomationElement.IsKeyboardFocusableProperty);
if (isKeyboardFocusableProperty is bool)
{
isKeyboardFocusable = (bool)isKeyboardFocusableProperty;
}
// UIAutomation already focuses the containing HWND for us, so only need to
// set focus on the item within that...
if (isKeyboardFocusable)
{
// Then set the focus on this item (virtual methods)
SetFocus();
return;
}
throw new InvalidOperationException(SR.SetFocusFailed);
}
#endregion
//------------------------------------------------------
//
// Internal Methods
//
//------------------------------------------------------
#region Internal Methods
// Returns the clickable point on the element
// In the case when clickable point is obtained - method returns true
// In the case when clickable point cannot be obtained - method returns false
internal bool GetClickablePoint(out NativeMethods.Win32Point pt, bool fClipClientRect)
{
NativeMethods.Win32Rect rcItem = new NativeMethods.Win32Rect(BoundingRectangle);View on GitHub (pinned to 81131a70a4)