WPF: Buttons sometimes aren't reported as control elements

This article is referenced directly by Microsoft Accessibility Insights for Windows. Microsoft Accessibility Insights for Windows can help spotlight many accessibility issues in UI, and guide you to the relevant UI framework-specific code sample to help you resolve the issue. Learn more about Microsoft Accessibility Insights for Windows.

Error message

This issue can cause the following error message in Accessibility Insights for Windows: The given ControlType must have the IsControlElement property set to TRUE.

How to investigate

The behavior of .NET Framework versions before .NET Framework 4.8.1 created situations where buttons could exist in the UI Automation hierarchy without existing in the visual interface, which sometimes caused the IsControlElementTrue property to be false on WPF buttons. There is no GitHub issue tracking this change, but it is documented on Preventing a screen reader from encountering an element. This behavior is fixed in .NET Framework 4.8.1, as well as in .NET Core 3.1 or later.

If the IsControlElementTrueRequired rule fails in an application that is already targeting a fixed version, then you may have a problem in your code. Look for classes that derive from objects in the System.Windows.Automation.Peers namespace to see if you are providing customized accessibility implementations.

Possible workarounds

To take advantage of the fixed behavior, you need to enable it. You can do this in two ways:

  1. Target a fixed version of .NET Core or .NET Framework
  2. If your application uses .NET Framework but targets a version older than 4.8.1, you can add the following AppContextSwitchOverrides to your app.config file to enable the new behavior on machines where .NET Framework 4.8.1 is installed:
  <runtime>
    <AppContextSwitchOverrides value="Switch.UseLegacyAccessibilityFeatures=false;Switch.UseLegacyAccessibilityFeatures.2=false;Switch.UseLegacyAccessibilityFeatures.3=false;Switch.UseLegacyAccessibilityFeatures.4=false;Switch.UseLegacyAccessibilityFeatures.5=false;" />
  </runtime>