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

  1. Ensure the parent RibbonMenuButton's submenu is open so the RibbonMenuItem container is realized before calling Expand.
  2. Use the automation peer's ExpandCollapseState pattern on a realized element, or scroll/realize the item via ScrollItemPattern first.
  3. Catch ElementNotAvailableException and retry after realizing the element.
  4. 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

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


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)