Narrator reads collapsed content

WPF, hide collapsed content to be ignored by the narrator

Hidden maintains

Last week, I worked on some accessibility issues. One of them was the windows narrator reading collapsed content. Hidding the content from the narrator was a little harder than I would have expected when starting working on the issues.

The WPF application is using an MVVM pattern where controls are hidden and shown by changing the Visibility property. For normal use, this works fine. However, when navigating the screen with the windows narrator, the collapsed content was still read to the user. When looking into the documentation, content in WPF that is collapsed is removed from the visual tree and should not be read by the narrator. As digging deeper it seems to be a bug in the Framework version we were using. Unfortunately, we can’t dictate to our users what framework version to use. .NET Framework version 4.8 will fix the bug, but our users will use the application from 4.7.1.

Fortunately, I found some hints to make a workaround. By overriding the OnCreateAutomationPeer method, you can give a hint to the narrator to read or not read the element. The method IsControlElementCore in the AutomationPeer class gives a hint on if a control is a Control element if so the narrator skips the control. This can be used to skip your content when hidden:

public class MyButton : Button
    protected override AutomationPeer OnCreateAutomationPeer()
        return new MyAutomationPeer(this);

public class MyAutomationPeer : ButtonBaseAutomationPeer
     public MyAutomationPeer(Button button): base(button) {}

     protected override bool IsControlElementCore()
          return Owner.Visible != Visiblity.Collapsed;

In the XAML I updated the controls to my own controls and now the collapsed content is hidden from the narrator. When we are able to only support .NET Framework 4.8 the custom implementations can be removed from the code base. Till then the workaround makes the page accessible for screen readers.

Some links:

Image by:Malcolm Lightbody

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.