{"record":{"id":"76f5e80dde203860","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-virtualizedelement","errorCode":null,"errorMessage":"Microsoft.Windows.Controls.SR.VirtualizedElement","messagePattern":"Microsoft\\.Windows\\.Controls\\.SR\\.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":171,"sourceCode":"\n                menuItemOwner.IsSubmenuOpen = true;\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);\n            }\n        }\n\n        ///\n        void IExpandCollapseProvider.Collapse()\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 = false;\n            }\n            else\n            {\n                throw new InvalidOperationException(Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed);","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Automation/Peers/RibbonMenuItemDataAutomationPeer.cs#L153-L189","documentation":"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.","triggerScenarios":"Calling Collapse() on a RibbonMenuItemDataAutomationPeer whose wrapper UIElement is null because the item's container was never realized or was recycled by virtualization.","commonSituations":"Automation scripts collapsing all open submenus by iterating peers, where some items have no realized containers; containers discarded after the submenu was closed programmatically.","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."],"exampleFix":"// before\ncachedExpandCollapsePattern.Collapse();\n// after\ntry { cachedExpandCollapsePattern.Collapse(); }\ncatch (ElementNotAvailableException) { cachedElement = reFindElement(); }","handlingStrategy":"try-catch","validationCode":"if (element != null && !element.IsOffscreen) collapse();","typeGuard":"if (peer.GetWrapper() is RibbonMenuItem) { /* element realized */ }","tryCatchPattern":"try { expandCollapsePattern.Collapse(); }\ncatch (ElementNotAvailableException) { /* element virtualized; skip or re-find */ }","preventionTips":["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"],"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"}