dotnet/wpf · error · ElementNotAvailableException
ElementNotAvailableException
Error message
ElementNotAvailableException
What it means
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.
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.
Example fix
// before
invokePattern.SetValue(dateText);
// after
if (calendarElement.Current.IsOffscreen == false)
{
invokePattern.SetValue(dateText);
}
else
{
calendarElement = treeWalker.GetParent(...); // refresh/re-find the element
} Defensive patterns
Strategy: retry
Validate before calling
if (calendarElement != null && !calendarElement.Current.IsOffscreen) { /* proceed */ } Type guard
static bool IsAlive(AutomationElement e) { try { var _ = e.Current.Name; return true; } catch (ElementNotAvailableException) { return false; } } Try / catch
try { /* calendar input operation */ } catch (ElementNotAvailableException) { calendarElement = ReFindElement(); /* then retry */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- E_OUTOFMEMORY
- Element no longer available.
- Element no longer available.
- ElementNotAvailableException
- ElementNotAvailableException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b26ec1af39fbdc25.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs:2211
}
}
}
// Detect if we're in the menu mode.
private bool InCalendarMenuMode()
{
NativeMethods.GUITHREADINFO gui;
uint processId;
uint threadId = Misc.GetWindowThreadProcessId(_hwnd, out processId);
return (Misc.ProxyGetGUIThreadInfo(threadId, out gui)
&& (Misc.IsBitSet(gui.dwFlags, NativeMethods.GUI_POPUPMENUMODE)));
}
private bool MakeReadyForInput()
{
if (!SafeNativeMethods.IsWindowVisible(_hwnd))
{
throw new ElementNotAvailableException();
}
NativeMethods.GUITHREADINFO gui;
if (Misc.ProxyGetGUIThreadInfo(0, out gui) && _hwnd == gui.hwndActive)
{
return true;
}
// try to set focus
return Misc.SetFocus(_hwnd);
}
#endregion ExpandCollapse Helper
#endregion
}
View on GitHub (pinned to 81131a70a4)