Skip to content

Commit

Permalink
Fix #122 - NullReferenceException in ItemAutomationPeer.GetNameCore() (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
KirillOsenkov committed Dec 14, 2022
1 parent ebc60c8 commit 9101c84
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 9101c84

Please sign in to comment.