diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridColumnHeadersPresenterAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridColumnHeadersPresenterAutomationPeer.cs index 90d79ea8788..09c2cd62d86 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridColumnHeadersPresenterAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridColumnHeadersPresenterAutomationPeer.cs @@ -131,7 +131,7 @@ protected override List GetChildrenCore() // protection from indistinguishable items - for example, 2 strings with same value // this scenario does not work in ItemsControl however is not checked for. - if (ItemPeers[dataItem] == null) + if (peer != null && ItemPeers[dataItem] == null) { children.Add(peer); ItemPeers[dataItem] = peer; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GroupItemAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GroupItemAutomationPeer.cs index a09f0d8194e..ef36a6c382b 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GroupItemAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/GroupItemAutomationPeer.cs @@ -274,8 +274,8 @@ protected override List GetChildrenCore() // but only if we haven't added a peer for this item during this GetChildrenCore call. bool itemMissingPeerInGlobalStorage = itemsControlAP.ItemPeers[item] == null; - if (itemMissingPeerInGlobalStorage - || (peer?.GetParent() == this && addedChildren[item] == null)) + if (peer != null && (itemMissingPeerInGlobalStorage + || (peer.GetParent() == this && addedChildren[item] == null))) { children.Add(peer); addedChildren[item] = peer; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs index 890eb4f1a41..df43ac5f2cd 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ItemsControlAutomationPeer.cs @@ -199,7 +199,7 @@ protected override List GetChildrenCore() // protection from indistinguishable items - for example, 2 strings with same value // this scenario does not work in ItemsControl however is not checked for. - if (_dataChildren[dataItem] == null) + if (peer != null && _dataChildren[dataItem] == null) { children.Add(peer); _dataChildren[dataItem] = peer;