dotnet/wpf · error · ElementNotAvailableException

SR.VirtualizedElement

Error message

SR.VirtualizedElement

What it means

DateTimeAutomationPeer (Calendar day/month button peers) throws ElementNotAvailableException when the owning button element no longer exists because it was virtualized out of the visual tree. UIA properties that require the live element (e.g. Grid.Column of the day cell) cannot be answered for a virtualized element.

Solutions

  1. Re-acquire the calendar day peers via a fresh FindAll/GetChildren after any view change.
  2. Hold element references only while the calendar view is stable.
  3. Subscribe to Calendar DisplayMode/DisplayDate changes and refresh cached peers.
  4. Catch ElementNotAvailableException and treat the element as gone (COM UIA maps this to UIA_E_ELEMENTNOTAVAILABLE).

Example fix

// before
var col = cachedDayPeer.GetProperty(Grid.ColumnProperty); // stale peer
// after
try { var col = cachedDayPeer.GetProperty(Grid.ColumnProperty); }
catch (ElementNotAvailableException) { cachedDayPeer = calendar.FindChildren().First(p => IsSameDay(p, day)); }
Defensive patterns

Strategy: try-catch

Validate before calling

bool alive = dayButton != null && dayButton.IsVisible && (PresentationSource.FromVisual(dayButton) != null);

Type guard

bool IsElementAvailable(AutomationPeer p) => p?.GetPeerFromPoint() != null; // or check owner button not null

Try / catch

try { value = peer.GetCurrentPropertyValue(prop); }
catch (ElementNotAvailableException) { RefreshPeers(); }

Prevention

When it happens

Trigger: Querying automation properties (Grid.Column/Row of a calendar day) on a DateTimeAutomationPeer after the Calendar virtualized its children — e.g. the calendar view was switched, collapsed, or scrolled and the button was recycled.

Common situations: UIA clients holding day-cell peers across Calendar DisplayMode changes or month navigation; elements retrieved while the Calendar was in a tab that later got virtualized.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DateTimeAutomationPeer.cs:638

        #endregion AutomationPeer override Methods

        #region IGridItemProvider

        /// <summary>
        /// Grid item column.
        /// </summary>
        int IGridItemProvider.Column
        {
            get
            {
                Button owningButton = OwningButton;
                if (owningButton != null)
                {
                    return (int)owningButton.GetValue(Grid.ColumnProperty);
                }
                else
                {
                    throw new ElementNotAvailableException(SR.VirtualizedElement);
                }
            }
        }

        /// <summary>
        /// Grid item column span.
        /// </summary>
        int IGridItemProvider.ColumnSpan
        {
            get
            {
                Button owningButton = OwningButton;
                if (owningButton != null)
                {
                    return (int)owningButton.GetValue(Grid.ColumnSpanProperty);
                }
                else
                {

View on GitHub (pinned to 81131a70a4)