{"record":{"id":"f793428a0485b338","repo":"dotnet/wpf","slug":"sr-virtualizedelement-itemautomationpeer","errorCode":null,"errorMessage":"SR.VirtualizedElement","messagePattern":"SR\\.VirtualizedElement","errorType":"exception","errorClass":"ElementNotAvailableException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemAutomationPeer.cs","lineNumber":125,"sourceCode":"                if(wrapperPeer == null) //fall back to default peer if there is no specific one\n                {\n                    if(wrapper is FrameworkElement)\n                        wrapperPeer = new FrameworkElementAutomationPeer((FrameworkElement)wrapper);\n                    else\n                        wrapperPeer = new UIElementAutomationPeer(wrapper);\n                }\n            }\n\n            return wrapperPeer;\n        }\n\n        /// <summary>\n        internal 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 && !(this is GridViewItemAutomationPeer) && !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\n        ///\n        internal override bool IgnoreUpdatePeer()\n        {\n            // Ignore UpdatePeer if the we're no longer in the automation tree.\n            // There's no need to update such a peer, as it no longer\n            // participates in automation.  And UpdatePeer actually throws exceptions\n            // in some cases.","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemAutomationPeer.cs#L107-L143","documentation":"ItemAutomationPeer.ThrowElementNotAvailableException throws ElementNotAvailableException(SR.VirtualizedElement) when UIA queries a peer whose item is virtualized (not realized in the automation tree). This only fires when the VirtualizedItem pattern is available, the peer is not a GridViewItemAutomationPeer, and the peer is not attached to its parent's children. It signals the client that the element must be realized (e.g. via ScrollItemPattern.ScrollIntoView) before it can be used.","triggerScenarios":"UIA client accesses properties or patterns of an item in a virtualized ItemsControl (ListBox/ListView/DataGrid with virtualization on) while the item is scrolled out of view and not in the automation tree.","commonSituations":"UIA tests addressing list items by index in large lists without scrolling the item into view; older/unpatched UIA clients that don't handle VirtualizedItem pattern; accessing DataGrid rows in virtualization modes after data changes.","solutions":["Call VirtualizedItemPattern.Realize() (or ScrollItemPattern.ScrollIntoView()) on the element before reading its properties.","Scroll the container into view in the app (BringIntoView) or disable UI virtualization if the list is small (VirtualizingStackPanel.VirtualizationMode / ScrollViewer.CanContentScroll=false).","In UIA clients, treat UIA_E_ELEMENTNOTAVAILABLE as a cue to realize/scroll and retry.","Catch ElementNotAvailableException in code that iterates ItemsControl items and skip or realize the item."],"exampleFix":"// before\nvar name = listItem.Current.Name; // may throw element-not-available\n// after\nvar virt = listItem.GetCurrentPattern(VirtualizedItemPattern.Pattern) as VirtualizedItemPattern;\nvirt?.Realize();\nvar name = listItem.Current.Name;","handlingStrategy":"try-catch","validationCode":"if (element.GetCurrentPattern(VirtualizedItemPattern.Pattern) is VirtualizedItemPattern v) v.Realize();","typeGuard":"static bool IsAvailable(AutomationElement e) => !e.Current.IsOffscreen || e.TryGetCurrentPattern(VirtualizedItemPattern.Pattern, out _);","tryCatchPattern":"try { ReadElementProperties(item); } catch (ElementNotAvailableException) { item.GetPattern(VirtualizedItemPattern).Realize(); ReadElementProperties(item); }","preventionTips":["Realize/scroll virtualized items into view before reading properties.","Handle UIA_E_ELEMENTNOTAVAILABLE as a realize-and-retry signal in UIA clients.","Disable virtualization for small lists used by automation tests.","Use VirtualizedItemPattern.Realize() instead of raw index addressing."],"tags":["wpf","ui-automation","virtualization","element-not-available"],"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"}