{"record":{"id":"4910b605d8eaaf73","repo":"dotnet/wpf","slug":"sr-setfocusfailed","errorCode":null,"errorMessage":"SR.SetFocusFailed","messagePattern":"SR\\.SetFocusFailed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/ContentElementAutomationPeer.cs","lineNumber":307,"sourceCode":"        {\n            return AutomationProperties.GetHeadingLevel(_owner);\n        }\n\n        /// <summary>\n        /// <see cref=\"AutomationPeer.GetClickablePointCore\"/>\n        /// </summary>\n        protected override Point GetClickablePointCore()\n        {\n            return new Point(double.NaN, double.NaN);\n        }\n\n        /// <summary>\n        /// <see cref=\"AutomationPeer.SetFocusCore\"/>\n        /// </summary>\n        protected override void SetFocusCore()\n        {\n            if (!_owner.Focus())\n                throw new InvalidOperationException(SR.SetFocusFailed);\n        }\n\n        ///\n        internal override Rect GetVisibleBoundingRectCore()\n        {\n            return GetBoundingRectangle();\n        }\n        \n\n        private ContentElement _owner;\n        private SynchronizedInputAdaptor _synchronizedInputPattern;\n    }\n}\n","sourceCodeStart":289,"sourceCodeEnd":321,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/ContentElementAutomationPeer.cs#L289-L321","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ncontentElementAutomationPeer.SetFocus();\n// after\nif (contentElement is ContentElement ce && ce.Focusable)\n{\n    ce.Focus();\n}\nelse\n{\n    // handle non-focusable element\n}","handlingStrategy":"try-catch","validationCode":"bool canFocus = contentElement is ContentElement ce && ce.Focusable && ce.IsVisible && ce.IsEnabled;","typeGuard":"static bool CanSetFocus(ContentElement e) => e is { Focusable: true, IsVisible: true, IsEnabled: true };","tryCatchPattern":"try\n{\n    peer.SetFocus();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"focus\"))\n{\n    // fall back: use FocusManager or select the element instead\n}","preventionTips":["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"],"tags":["wpf","ui-automation","focus"],"backgroundTag":"focus-operation-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}