From 9101c848312969ef9d616baf2b2213fd684efba7 Mon Sep 17 00:00:00 2001 From: Kirill Osenkov Date: Wed, 14 Dec 2022 02:18:12 -0800 Subject: [PATCH] Fix #122 - NullReferenceException in ItemAutomationPeer.GetNameCore() (#7115) For virtualized tree views it can happen that we are calling GetNameCore() on an item that has been scrolled out of view and doesn't have an associated container. ItemContainerGenerator.GetContainerFromItem() returns null, and so ItemsControlAutomationPeer ends up being null as well. We're still going to retrieve the name from the item.ToString(), so it's much better than crashing here and aborting the entire layout pass. --- .../System/Windows/Automation/Peers/ItemAutomationPeer.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemAutomationPeer.cs index ebc47e91afc..de6f5691f63 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemAutomationPeer.cs @@ -527,9 +527,10 @@ protected override string GetNameCore() { name = wrapperPeer.GetName(); } - else if (item != null) + // see https://github.com/dotnet/wpf/issues/122 + else if (item != null && ItemsControlAutomationPeer is { } itemsControlAutomationPeer) { - using (RecyclableWrapper recyclableWrapper = ItemsControlAutomationPeer.GetRecyclableWrapperPeer(item)) + using (RecyclableWrapper recyclableWrapper = itemsControlAutomationPeer.GetRecyclableWrapperPeer(item)) { name = recyclableWrapper.Peer.GetName(); }