{"record":{"id":"99930870396e7463","repo":"dotnet/wpf","slug":"sr-setfocusfailed-uielementautomationpeer","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/UIElementAutomationPeer.cs","lineNumber":517,"sourceCode":"            // If the source isn't an HwnSource, there's not much we can do, return (double.NaN, double.NaN) point\n            if(hwndSource == null)\n                return pt;\n\n            Rect rectElement    = new Rect(new Point(0, 0), _owner.RenderSize);\n            Rect rectRoot       = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);\n            Rect rectClient     = PointUtil.RootToClient(rectRoot, presentationSource);\n            Rect rectScreen     = PointUtil.ClientToScreen(rectClient, hwndSource);\n            \n            pt = new Point(rectScreen.Left + rectScreen.Width * 0.5, rectScreen.Top + rectScreen.Height * 0.5);\n\n            return pt;\n        }\n\n        ///\n        protected override void SetFocusCore() \n        { \n            if (!_owner.Focus())\n                throw new InvalidOperationException(SR.SetFocusFailed);\n        }\n\n        private UIElement _owner;\n        private SynchronizedInputAdaptor _synchronizedInputPattern;\n    }\n}\n\n","sourceCodeStart":499,"sourceCodeEnd":525,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Automation/Peers/UIElementAutomationPeer.cs#L499-L525","documentation":"UIElementAutomationPeer.SetFocusCore calls _owner.Focus() on the owning UIElement and throws InvalidOperationException (SR.SetFocusFailed) when the element cannot take keyboard focus. This is the standard peer used for most WPF controls, so this is the most common path for the UIA Focus pattern failure.","triggerScenarios":"UIA clients or code calling peer.SetFocus() on a control whose UIElement owner fails Focus(): Focusable=false (e.g. TextBlock, panels), control disabled, hidden/collapsed, or not yet loaded in a window.","commonSituations":"UI automation tests focusing labels or disabled buttons; focusing controls during window construction before the handle exists; screen-reader clients trying to focus decorative elements.","solutions":["Verify IsVisible, IsEnabled and Focusable on the target control before SetFocus","Focus the control directly with control.Focus() and check the bool return","Set focus via FocusManager.SetFocusedElement(focusScope, element)","In UIA clients, catch InvalidOperationException and fall back to selecting the element instead of focusing"],"exampleFix":"// before\npeer.SetFocus();\n// after\nif (button.Focusable && button.IsVisible && button.IsEnabled)\n{\n    button.Focus();\n}\nelse\n{\n    button.Focusable = true; // then retry focus\n    button.Focus();\n}","handlingStrategy":"try-catch","validationCode":"bool canFocus = control is UIElement u && u.Focusable && u.IsVisible && u.IsEnabled;","typeGuard":"static bool CanSetFocus(UIElement e) => e is { Focusable: true, IsVisible: true, IsEnabled: true };","tryCatchPattern":"try\n{\n    peer.SetFocus();\n}\ncatch (InvalidOperationException)\n{\n    FocusManager.SetFocusedElement(FocusManager.GetFocusScope(control), control);\n}","preventionTips":["Mark automatable controls Focusable=true (avoid TextBlock/Panel targets)","Call SetFocus only after the window is loaded and shown","Use control.Focus()'s bool return instead of the peer in in-process code","Handle focus failure gracefully in UIA clients (e.g. use SelectionItemPattern)"],"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"}