{"record":{"id":"b26ec1af39fbdc25","repo":"dotnet/wpf","slug":"elementnotavailableexception-windowscalendar","errorCode":null,"errorMessage":"ElementNotAvailableException","messagePattern":"ElementNotAvailableException","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs","lineNumber":2211,"sourceCode":"                    }\n                }\n            }\n\n            // Detect if we're in the menu mode.\n            private bool InCalendarMenuMode()\n            {\n                NativeMethods.GUITHREADINFO gui;\n                uint processId;\n                uint threadId = Misc.GetWindowThreadProcessId(_hwnd, out processId);\n                return (Misc.ProxyGetGUIThreadInfo(threadId, out gui)\n                            && (Misc.IsBitSet(gui.dwFlags, NativeMethods.GUI_POPUPMENUMODE)));\n            }\n\n            private bool MakeReadyForInput()\n            {\n                if (!SafeNativeMethods.IsWindowVisible(_hwnd))\n                {\n                    throw new ElementNotAvailableException();\n                }\n\n                NativeMethods.GUITHREADINFO gui;\n\n                if (Misc.ProxyGetGUIThreadInfo(0, out gui) && _hwnd == gui.hwndActive)\n                {\n                    return true;\n                }\n\n                // try to set focus\n                return Misc.SetFocus(_hwnd);\n            }\n\n            #endregion ExpandCollapse Helper\n\n            #endregion\n        }\n","sourceCodeStart":2193,"sourceCodeEnd":2229,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs#L2193-L2229","documentation":"WindowsCalendar.MakeReadyForInput() throws ElementNotAvailableException when the native calendar hwnd is not visible (SafeNativeMethods.IsWindowVisible(_hwnd) is false). In UIA, an element whose backing window has disappeared or is hidden is considered no longer available.","triggerScenarios":"Any calendar provider operation that requires input preparation (e.g. setting focus/selection) while the MonthCalendar window is hidden, on a background tab, or its parent window is minimized/closed.","commonSituations":"UIA scripts automating a calendar on a hidden form, a form that was closed between element lookup and invocation (stale element), or controls hidden by tabbed layouts.","solutions":["Ensure the calendar window is visible before performing the operation (show the form / select the tab).","Re-find the element and retry; a stale reference to a hidden or recreated control triggers this.","Handle ElementNotAvailableException in the UIA client and refresh the element from the automation tree.","Avoid automating controls on minimized or closed windows."],"exampleFix":"// before\ninvokePattern.SetValue(dateText);\n// after\nif (calendarElement.Current.IsOffscreen == false)\n{\n    invokePattern.SetValue(dateText);\n}\nelse\n{\n    calendarElement = treeWalker.GetParent(...); // refresh/re-find the element\n}","handlingStrategy":"retry","validationCode":"if (calendarElement != null && !calendarElement.Current.IsOffscreen) { /* proceed */ }","typeGuard":"static bool IsAlive(AutomationElement e) { try { var _ = e.Current.Name; return true; } catch (ElementNotAvailableException) { return false; } }","tryCatchPattern":"try { /* calendar input operation */ } catch (ElementNotAvailableException) { calendarElement = ReFindElement(); /* then retry */ }","preventionTips":["Re-find UIA elements after UI changes instead of caching stale references","Ensure the target window is visible (not minimized/hidden/closed) before automating","Handle ElementNotAvailableException globally in UIA clients with refresh-and-retry"],"tags":["uiautomation","stale-element","hidden-window"],"backgroundTag":"resource-not-found","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"}