Null reference exception when running a .NET app with UI automation

This article helps you resolve the null reference exception that's thrown when you run a .NET application with UI automation.

Original product version:   Microsoft .NET Framework
Original KB number:   2653004

Symptoms

You may see a null reference exception thrown when running an application that contains a .NET Calendar control and any UI automation tool like Inspect.exe. The application may crash when focus goes to the Calendar control.

Cause

You may have a subtle issue in your app defined template where the null values in the AutomationPeer collection stemmed.

The resources within a theme level dictionary that will be referenced outside need to be named using a ComponentResourceKey. In this case, UIAutomation (Inspect.exe) wants to reference this DataTemplate and raise the exception.

Resolution

Set the DataTamplate Key as follows:

<DataTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly=CalendarItem, ResourceId=DayTitleTemplate}">

Instead of:

<DataTemplate x:Key="DayTitleTemplate">