{"record":{"id":"0ebabf8fe9facb97","repo":"dotnet/wpf","slug":"virtualizedelement","errorCode":null,"errorMessage":"VirtualizedElement","messagePattern":"VirtualizedElement","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs","lineNumber":140,"sourceCode":"                }\n            }\n\n            return result;\n        }\n\n        #endregion\n\n        #region IExpandCollapseProvider Members\n\n        void IExpandCollapseProvider.Expand()\n        {\n            if (!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            UIElement owner = GetWrapper();\n            if (owner == null)\n            {\n                throw new ElementNotAvailableException(Microsoft.Windows.Controls.SR.VirtualizedElement);\n            }\n\n            RibbonMenuItem menuItemOwner = owner as RibbonMenuItem;\n            if (menuItemOwner != null)\n            {\n                MenuItemRole role = menuItemOwner.Role;\n\n                if ((role != MenuItemRole.TopLevelHeader && role != MenuItemRole.SubmenuHeader)\n                    || !menuItemOwner.HasItems)\n                {\n                    throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n                }\n\n                menuItemOwner.IsSubmenuOpen = true;\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs#L122-L158","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n((ExpandCollapsePattern)menuItemAutomation.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();\n// after\nvar parent = ribbonMenuButtonAutomation.FindFirst(TreeScope.Children, Condition.TrueCondition);\n((ExpandCollapsePattern)ribbonMenuButtonAutomation.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand(); // realize items\n((ExpandCollapsePattern)menuItemAutomation.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand();","handlingStrategy":"try-catch","validationCode":"var realized = itemAutomation != null && !itemAutomation.IsOffscreen; // heuristics","typeGuard":"if (RibbonMenuItemAutomationPeer.GetWrapper() is UIElement owner && owner is RibbonMenuItem) { /* safe */ }","tryCatchPattern":"try { expandCollapsePattern.Expand(); }\ncatch (ElementNotAvailableException) { /* re-realize element, then retry */ }","preventionTips":["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"],"tags":["wpf","uia","automation","virtualization"],"backgroundTag":"entity-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}