{"record":{"id":"523f76551e433a04","repo":"dotnet/wpf","slug":"sr-virtualizedelement","errorCode":null,"errorMessage":"SR.VirtualizedElement","messagePattern":"SR\\.VirtualizedElement","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridCellItemAutomationPeer.cs","lineNumber":802,"sourceCode":"        #endregion\n\n        #region Private Methods\n\n        private void EnsureEnabled()\n        {\n            if (!OwningDataGrid.IsEnabled)\n            {\n                throw new ElementNotEnabledException();\n            }\n        }\n\n        /// <summary>\n        private void ThrowElementNotAvailableException()\n        {\n            // To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core\n            // avoids throwing exception and provide older behavior returning default values for items which are virtualized rather than throwing exception.\n            if (VirtualizedItemPatternIdentifiers.Pattern != null && !IsItemInAutomationTree())\n                throw new ElementNotAvailableException(SR.VirtualizedElement);\n        }\n\n        private bool IsItemInAutomationTree()\n        {\n            AutomationPeer parent = this.GetParent();\n            if (this.Index != -1 && parent != null && parent.Children != null && this.Index < parent.Children.Count && parent.Children[this.Index] == this)\n                return true;\n            else return false;\n        }\n\n        #endregion\n\n        #region Private Properties\n\n        private bool IsCellSelectionUnit\n        {\n            get\n            {","sourceCodeStart":784,"sourceCodeEnd":820,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridCellItemAutomationPeer.cs#L784-L820","documentation":"DataGridCellItemAutomationPeer.ThrowElementNotAvailableException throws ElementNotAvailableException (SR.VirtualizedElement) when the cell's element is virtualized — i.e., the peer is not currently present in the automation tree (IsItemInAutomationTree() is false) and the VirtualizedItemPattern is available. This guards clients against querying peers for UI elements that WPF has virtualized out of the visual tree.","triggerScenarios":"Accessing any property/pattern on a DataGrid cell peer whose row or column is scrolled outside the DataGrid's realized window (row/column virtualization), causing IsItemInAutomationTree to return false; occurs when ThrowElementNotAvailableException is called from property getters such as GetChildrenCore or pattern accessors.","commonSituations":"UIA tests iterating all cells of a large DataGrid where most rows are virtualized; screen readers querying off-screen cells; environments with older unmanaged UIAutomationCore (there the exception is deliberately skipped and defaults are returned instead).","solutions":["Call IVirtualizedItemProvider.Realize() (scroll the item into view) before accessing cell properties.","Set VirtualizationMode or disable virtualization (EnableRowVirtualization=false / EnableColumnVirtualization=false) for small grids used in tests.","Catch ElementNotAvailableException and treat the element as stale, retrying after scrolling into view.","In tests, scroll each row into view (ScrollIntoView) before querying its cells."],"exampleFix":"// before\nvar name = cellPeer.GetName(); // ElementNotAvailableException\n\n// after\n((IVirtualizedItemProvider)cellPeer).Realize();\nvar name = cellPeer.GetName();","handlingStrategy":"try-catch","validationCode":"if (!peer.IsOffscreen) { /* safe to query */ }","typeGuard":"bool IsRealized(AutomationPeer peer) { try { var _ = peer.GetParent(); return true; } catch (ElementNotAvailableException) { return false; } }","tryCatchPattern":"try { ((IVirtualizedItemProvider)peer).Realize(); AccessCell(peer); } catch (ElementNotAvailableException) { /* retry after scroll into view */ }","preventionTips":["Realize/ScrollIntoView before querying virtualized cells","Catch ElementNotAvailableException as stale-element","Disable row/column virtualization for small test grids","Re-query peers after scrolling (they may be recreated)"],"tags":["wpf","automation","datagrid","virtualization","stale-element"],"backgroundTag":"stale-element-reference","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}