{"record":{"id":"6407be8aef438c51","repo":"dotnet/wpf","slug":"sr-setfocusfailed-calendarautomationpeer","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/CalendarAutomationPeer.cs","lineNumber":197,"sourceCode":"                if (!owner.Focus())\n                {\n                    DateTime focusedDate;\n                    // Focus should have moved to either SelectedDate or DisplayDate\n                    if (owner.SelectedDate.HasValue && DateTimeHelper.CompareYearMonth(owner.SelectedDate.Value, owner.DisplayDateInternal) == 0)\n                    {\n                        focusedDate = owner.SelectedDate.Value;\n                    }\n                    else\n                    {\n                        focusedDate = owner.DisplayDate;\n                    }\n\n                    DateTimeAutomationPeer focusedItem = GetOrCreateDateTimeAutomationPeer(focusedDate, owner.DisplayMode, /*addParentInfo*/ false);\n                    FrameworkElement focusedButton = focusedItem.OwningButton;\n\n                    if (focusedButton == null || !focusedButton.IsKeyboardFocused)\n                    {\n                        throw new InvalidOperationException(SR.SetFocusFailed);\n                    }\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.SetFocusFailed);\n            }\n        }\n\n        #endregion Protected Methods\n\n        #region InternalMethods\n\n        private DateTimeAutomationPeer GetOrCreateDateTimeAutomationPeer(DateTime date, CalendarMode buttonMode)\n        {\n            return GetOrCreateDateTimeAutomationPeer(date, buttonMode, /*addParentInfo*/ true);\n        }\n        ","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/CalendarAutomationPeer.cs#L179-L215","documentation":"CalendarAutomationPeer.SetFocusCore throws InvalidOperationException(SR.SetFocusFailed) when UI Automation requested keyboard focus but, after the attempt, the focused day button could not be resolved or did not actually receive keyboard focus (focusedButton == null or IsKeyboardFocused is false). It is a post-condition check that the focus operation failed.","triggerScenarios":"Calling SetFocus() on a Calendar or DateTimeAutomationPeer child while the calendar is not focused, the focused date's button cannot be created, or focus moved elsewhere between the attempt and the verification.","commonSituations":"UIA tests calling SetFocus on Calendar dates right after changing DisplayMode; another control stealing focus concurrently; automation running while the calendar is mid-layout or virtualized.","solutions":["Ensure the Calendar has keyboard focus and a valid FocusedDate/DisplayMode before calling SetFocus.","Call SetFocus on the Calendar control itself first, then on the specific date peer.","Retry after layout completes (Dispatcher.BeginInvoke at Loaded priority) rather than immediately after changing DisplayMode.","Catch InvalidOperationException in the automation code and retry once focus stabilizes."],"exampleFix":"// before\ndatePeer.SetFocus(); // may throw if calendar not focused\n// after\ncalendar.Focus();\nDispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => datePeer.SetFocus()));","handlingStrategy":"try-catch","validationCode":"if (!calendar.IsKeyboardFocused || calendar.FocusedDate == null)\n{\n    calendar.Focus(); // establish focus before SetFocus on the peer\n}","typeGuard":null,"tryCatchPattern":"try { datePeer.SetFocus(); }\ncatch (InvalidOperationException)\n{\n    calendar.Focus();\n    Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => datePeer.SetFocus()));\n}","preventionTips":["Ensure the Calendar has keyboard focus before automating dates.","Let layout and DisplayMode changes settle before SetFocus.","Avoid concurrent focus changes during automation."],"tags":["wpf","ui-automation","calendar","keyboard-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"}