dotnet/wpf · error · ElementNotAvailableException
Microsoft.Windows.Controls.SR.VirtualizedElement
Error message
Microsoft.Windows.Controls.SR.VirtualizedElement
What it means
IExpandCollapseProvider.Collapse on RibbonMenuItemDataAutomationPeer mirrors Expand: if GetWrapper() returns null the RibbonMenuItem container is not realized (virtualized or submenu closed), and the peer throws ElementNotAvailableException with SR.VirtualizedElement.
Solutions
- Verify the element is available (e.g. via AutomationElement.GetCurrentPropertyValue or TryGetPattern) and realize it by opening the parent menu before Collapse.
- Walk the live UIA tree and only collapse elements that currently exist in the tree.
- Catch ElementNotAvailableException around Collapse and skip/refresh stale peer references.
- Re-acquire the AutomationElement from the UIA tree instead of caching peers across virtualization changes.
Example fix
// before
cachedExpandCollapsePattern.Collapse();
// after
try { cachedExpandCollapsePattern.Collapse(); }
catch (ElementNotAvailableException) { cachedElement = reFindElement(); } Defensive patterns
Strategy: try-catch
Validate before calling
if (element != null && !element.IsOffscreen) collapse();
Type guard
if (peer.GetWrapper() is RibbonMenuItem) { /* element realized */ } Try / catch
try { expandCollapsePattern.Collapse(); }
catch (ElementNotAvailableException) { /* element virtualized; skip or re-find */ } Prevention
- Collapse elements that are currently live in the UIA tree only
- Re-acquire AutomationElements after any menu open/close
- Avoid iterating cached peers across virtualization events
When it happens
Trigger: Calling Collapse() on a RibbonMenuItemDataAutomationPeer whose wrapper UIElement is null because the item's container was never realized or was recycled by virtualization.
Common situations: Automation scripts collapsing all open submenus by iterating peers, where some items have no realized containers; containers discarded after the submenu was closed programmatically.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- VirtualizedElement
- Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
- SR.Automation_RecursivePublicCall
- SR.VirtualizedElement
- AnchorItem ' ' does not have a realized container and hence…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/76f5e80dde203860.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs:171
menuItemOwner.IsSubmenuOpen = true;
}
else
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);
}
}
///
void IExpandCollapseProvider.Collapse()
{
if (!IsEnabled())
throw new ElementNotEnabledException();
UIElement owner = GetWrapper();
if (owner == null)
{
throw new ElementNotAvailableException(Microsoft.Windows.Controls.SR.VirtualizedElement);
}
RibbonMenuItem menuItemOwner = owner as RibbonMenuItem;
if (menuItemOwner != null)
{
MenuItemRole role = menuItemOwner.Role;
if ((role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)
|| !menuItemOwner.HasItems)
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);
}
menuItemOwner.IsSubmenuOpen = false;
}
else
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);View on GitHub (pinned to 81131a70a4)