{"record":{"id":"f606945d19f5a953","repo":"dotnet/wpf","slug":"target-element-cannot-receive-focus","errorCode":null,"errorMessage":"Target element cannot receive focus.","messagePattern":"Target element cannot receive focus\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/ProxySimple.cs","lineNumber":552,"sourceCode":"            // a SetFocus() was called on them, the case to bool will cause a NullReferenceException.\n            // So make sure the return is of type bool before casting.\n            bool isKeyboardFocusable = true;\n            object isKeyboardFocusableProperty = GetElementProperty(AutomationElement.IsKeyboardFocusableProperty);\n            if (isKeyboardFocusableProperty is bool)\n            {\n                isKeyboardFocusable = (bool)isKeyboardFocusableProperty;\n            }\n\n            // UIAutomation already focuses the containing HWND for us, so only need to\n            // set focus on the item within that...\n            if (isKeyboardFocusable)\n            {\n                // Then set the focus on this item (virtual methods)\n                SetFocus();\n                return;\n            }\n\n            throw new InvalidOperationException(SR.SetFocusFailed);\n        }\n\n        #endregion\n\n        //------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //------------------------------------------------------\n\n        #region Internal Methods\n\n        // Returns the clickable point on the element\n        // In the case when clickable point is obtained - method returns true\n        // In the case when clickable point cannot be obtained - method returns false\n        internal bool GetClickablePoint(out NativeMethods.Win32Point pt, bool fClipClientRect)\n        {\n            NativeMethods.Win32Rect rcItem = new NativeMethods.Win32Rect(BoundingRectangle);","sourceCodeStart":534,"sourceCodeEnd":570,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/ProxySimple.cs#L534-L570","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nlabelProxy.SetFocus(); // InvalidOperationException: Target element cannot receive focus.\n\n// after\nvar ae = (AutomationElement)labelProxy;\nif (ae.Current.IsKeyboardFocusable)\n{\n    ae.SetFocus();\n}\nelse\n{\n    // operate via patterns instead of focus\n    var invoke = ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;\n    invoke?.Invoke();\n}","handlingStrategy":"validation","validationCode":"static void EnsureFocusable(AutomationElement e)\n{\n    if (!e.Current.IsKeyboardFocusable)\n        throw new InvalidOperationException($\"{e.Current.AutomationId} is not keyboard focusable\");\n}","typeGuard":"static bool SupportsFocus(AutomationElement e) =>\n    e.Current.IsKeyboardFocusable &&\n    (e.Current.ControlType == ControlType.Button ||\n     e.Current.ControlType == ControlType.Edit ||\n     e.Current.ControlType == ControlType.Document ||\n     e.Current.ControlType == ControlType.ListItem);","tryCatchPattern":"try { element.SetFocus(); }\ncatch (InvalidOperationException)\n{\n    // element cannot receive focus; drive it via patterns or focus a parent\n    parentWindow.SetFocus();\n}","preventionTips":["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."],"tags":["uiautomation","focus","unsupported-operation"],"backgroundTag":"operation-not-supported","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}