{"record":{"id":"97570c9a3d148e5d","repo":"dotnet/wpf","slug":"sr-virtualizedelement-datetimeautomationpeer","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/DateTimeAutomationPeer.cs","lineNumber":638,"sourceCode":"        #endregion AutomationPeer override Methods\n\n        #region IGridItemProvider\n\n        /// <summary>\n        /// Grid item column.\n        /// </summary>\n        int IGridItemProvider.Column\n        {\n            get\n            {\n                Button owningButton = OwningButton;\n                if (owningButton != null)\n                {\n                    return (int)owningButton.GetValue(Grid.ColumnProperty);\n                }\n                else\n                {\n                    throw new ElementNotAvailableException(SR.VirtualizedElement);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Grid item column span.\n        /// </summary>\n        int IGridItemProvider.ColumnSpan\n        {\n            get\n            {\n                Button owningButton = OwningButton;\n                if (owningButton != null)\n                {\n                    return (int)owningButton.GetValue(Grid.ColumnSpanProperty);\n                }\n                else\n                {","sourceCodeStart":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DateTimeAutomationPeer.cs#L620-L656","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-acquire the calendar day peers via a fresh FindAll/GetChildren after any view change.","Hold element references only while the calendar view is stable.","Subscribe to Calendar DisplayMode/DisplayDate changes and refresh cached peers.","Catch ElementNotAvailableException and treat the element as gone (COM UIA maps this to UIA_E_ELEMENTNOTAVAILABLE)."],"exampleFix":"// before\nvar col = cachedDayPeer.GetProperty(Grid.ColumnProperty); // stale peer\n// after\ntry { var col = cachedDayPeer.GetProperty(Grid.ColumnProperty); }\ncatch (ElementNotAvailableException) { cachedDayPeer = calendar.FindChildren().First(p => IsSameDay(p, day)); }","handlingStrategy":"try-catch","validationCode":"bool alive = dayButton != null && dayButton.IsVisible && (PresentationSource.FromVisual(dayButton) != null);","typeGuard":"bool IsElementAvailable(AutomationPeer p) => p?.GetPeerFromPoint() != null; // or check owner button not null","tryCatchPattern":"try { value = peer.GetCurrentPropertyValue(prop); }\ncatch (ElementNotAvailableException) { RefreshPeers(); }","preventionTips":["Re-acquire calendar peers after DisplayMode/DisplayDate changes","Do not hold day-cell peer references across view switches","Handle UIA_E_ELEMENTNOTAVAILABLE in UIA clients as a normal virtualization outcome"],"tags":["wpf","uiautomation","calendar","virtualization"],"backgroundTag":"element-not-available","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"}