{"record":{"id":"6876c3f67e811201","repo":"dotnet/wpf","slug":"sr-setfocusfailed-datepickerautomationpeer","errorCode":null,"errorMessage":"SR.SetFocusFailed","messagePattern":"SR\\.SetFocusFailed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DatePickerAutomationPeer.cs","lineNumber":68,"sourceCode":"            return base.GetPattern(patternInterface);\n        }\n\n        #endregion Public Methods\n\n        #region Protected Methods\n\n        protected override void SetFocusCore()\n        {\n            DatePicker owner = OwningDatePicker;\n            if (owner.Focusable)\n            {\n                if (!owner.Focus())\n                {\n                    TextBox tb = owner.TextBox;\n                    //The focus should have gone to the TextBox inside DatePicker\n                    if (tb == null || !tb.IsKeyboardFocused)\n                    {\n                        throw new InvalidOperationException(SR.SetFocusFailed);\n                    }\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.SetFocusFailed);\n            }\n        }\n\n        /// <summary>\n        /// Gets the control type for the element that is associated with the UI Automation peer.\n        /// </summary>\n        /// <returns>The control type.</returns>\n        protected override AutomationControlType GetAutomationControlTypeCore()\n        {\n            return AutomationControlType.Custom;\n        }\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DatePickerAutomationPeer.cs#L50-L86","documentation":"DatePickerAutomationPeer.SetFocusCore throws InvalidOperationException when focus was requested on the DatePicker but neither the owner nor its internal TextBox ended up keyboard-focused. UIA's SetFocus contract requires focus to actually land, so the peer reports the failure rather than silently ignoring it.","triggerScenarios":"Calling SetFocus (IRawElementProviderSimple) on a DatePicker whose DatePickerTextBox cannot be located (owner.TextBox is null) or which did not take keyboard focus — e.g. the control is disabled, not visible, or in a non-activated window.","commonSituations":"UIA tests setting focus on a DatePicker inside a collapsed/unloaded tab or disabled control; windows without activation so Win32 Focus() succeeds but IsKeyboardFocused remains false.","solutions":["Ensure the DatePicker is visible, enabled, and its window is active before calling SetFocus.","Make sure the DatePicker's template (including its TextBox part) is loaded.","Activate the containing window (Window.Activate()/SetForegroundWindow) first.","Catch InvalidOperationException around SetFocus and retry after activating the window."],"exampleFix":"// before\nwindow.Show();\ndatePickerPeer.SetFocus(); // may fail if window not foreground\n// after\nwindow.Show();\nwindow.Activate();\nif (datePicker.IsVisible && datePicker.IsEnabled)\n    datePicker.Focus();","handlingStrategy":"try-catch","validationCode":"bool ready = datePicker.IsVisible && datePicker.IsEnabled && datePicker.IsLoaded && Window.GetWindow(datePicker).IsActive;","typeGuard":null,"tryCatchPattern":"try { datePicker.Focus(); }\ncatch (InvalidOperationException) { window.Activate(); datePicker.Focus(); }","preventionTips":["Activate the host window before SetFocus calls","Ensure the DatePicker template is applied (ApplyTemplate) before focusing","Skip SetFocus on disabled/hidden controls"],"tags":["wpf","uiautomation","datepicker","focus"],"backgroundTag":"invalid-state-transition","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"}