dotnet/wpf · error · ElementNotAvailableException

SR.VirtualizedElement

Error message

SR.VirtualizedElement

What it means

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.

Solutions

  1. Call IVirtualizedItemProvider.Realize() (scroll the item into view) before accessing cell properties.
  2. Set VirtualizationMode or disable virtualization (EnableRowVirtualization=false / EnableColumnVirtualization=false) for small grids used in tests.
  3. Catch ElementNotAvailableException and treat the element as stale, retrying after scrolling into view.
  4. In tests, scroll each row into view (ScrollIntoView) before querying its cells.

Example fix

// before
var name = cellPeer.GetName(); // ElementNotAvailableException

// after
((IVirtualizedItemProvider)cellPeer).Realize();
var name = cellPeer.GetName();
Defensive patterns

Strategy: try-catch

Validate before calling

if (!peer.IsOffscreen) { /* safe to query */ }

Type guard

bool IsRealized(AutomationPeer peer) { try { var _ = peer.GetParent(); return true; } catch (ElementNotAvailableException) { return false; } }

Try / catch

try { ((IVirtualizedItemProvider)peer).Realize(); AccessCell(peer); } catch (ElementNotAvailableException) { /* retry after scroll into view */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/523f76551e433a04. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridCellItemAutomationPeer.cs:802

        #endregion

        #region Private Methods

        private void EnsureEnabled()
        {
            if (!OwningDataGrid.IsEnabled)
            {
                throw new ElementNotEnabledException();
            }
        }

        /// <summary>
        private void ThrowElementNotAvailableException()
        {
            // To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core
            // avoids throwing exception and provide older behavior returning default values for items which are virtualized rather than throwing exception.
            if (VirtualizedItemPatternIdentifiers.Pattern != null && !IsItemInAutomationTree())
                throw new ElementNotAvailableException(SR.VirtualizedElement);
        }

        private bool IsItemInAutomationTree()
        {
            AutomationPeer parent = this.GetParent();
            if (this.Index != -1 && parent != null && parent.Children != null && this.Index < parent.Children.Count && parent.Children[this.Index] == this)
                return true;
            else return false;
        }

        #endregion

        #region Private Properties

        private bool IsCellSelectionUnit
        {
            get
            {

View on GitHub (pinned to 81131a70a4)