dotnet/wpf · error · ElementNotAvailableException
VirtualizedElement
Error message
VirtualizedElement
What it means
IExpandCollapseProvider.Expand on RibbonMenuItemDataAutomationPeer first resolves the realized UIElement via GetWrapper(). When the wrapper is null the RibbonMenuItem container has not been realized (it is virtualized), so the peer throws ElementNotAvailableException with SR.VirtualizedElement - the automation element refers to an item that currently has no live visual.
Solutions
- Ensure the parent RibbonMenuButton's submenu is open so the RibbonMenuItem container is realized before calling Expand.
- Use the automation peer's ExpandCollapseState pattern on a realized element, or scroll/realize the item via ScrollItemPattern first.
- Catch ElementNotAvailableException and retry after realizing the element.
- Disable container virtualization for the ribbon menu if automation access to all items is required.
Example fix
// before ((ExpandCollapsePattern)menuItemAutomation.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand(); // after var parent = ribbonMenuButtonAutomation.FindFirst(TreeScope.Children, Condition.TrueCondition); ((ExpandCollapsePattern)ribbonMenuButtonAutomation.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand(); // realize items ((ExpandCollapsePattern)menuItemAutomation.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();
Defensive patterns
Strategy: try-catch
Validate before calling
var realized = itemAutomation != null && !itemAutomation.IsOffscreen; // heuristics
Type guard
if (RibbonMenuItemAutomationPeer.GetWrapper() is UIElement owner && owner is RibbonMenuItem) { /* safe */ } Try / catch
try { expandCollapsePattern.Expand(); }
catch (ElementNotAvailableException) { /* re-realize element, then retry */ } Prevention
- Open the parent submenu before interacting with its items
- Do not cache automation peers of virtualized items
- Catch ElementNotAvailableException whenever touching ItemsControl-derived item peers
When it happens
Trigger: Calling Expand() on a RibbonMenuItemDataAutomationPeer whose underlying RibbonMenuItem container is not realized, e.g. the owning RibbonMenuButton submenu is closed or the item was virtualized out.
Common situations: UIA/test automation scripts expanding ribbon menu items before opening the parent menu; ItemsControl virtualization discarding the container between peer creation and the Expand call.
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
- Microsoft.Windows.Controls.SR.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/0ebabf8fe9facb97.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs:140
}
}
return result;
}
#endregion
#region IExpandCollapseProvider Members
void IExpandCollapseProvider.Expand()
{
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 = true;
}
else
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);View on GitHub (pinned to 81131a70a4)