Share via


WPF: Raising a focus event when tabbing to a custom control

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.

Problem

My app has a custom control, and the control has been build such that it's keyboard focusable. However, my when customer tabs to it, the Narrator screen reader does not follow keyboard focus to the control.

Suggested Fix

Narrator relies on UI Automation (UIA) FocusChanged events being raised as keyboard focus moves around the UI. If no event is being raised, then Narrator is not being made aware of the change in keyboard focus. So first run a UIA verification tool and determine if a UIA FocusChanged event is being raised. If the event is not being raised, then consider using the sample below to explicitly raise the event immediately after keyboard focus has moved to the custom control.

Code Sample: Raise a UIA FocusChanged event.

// In code behind.

AutomationPeer peer = FrameworkElementAutomationPeer.FromElement(MyCustomControl);
if (peer != null)
{
    peer.RaiseAutomationEvent(AutomationEvents.AutomationFocusChanged);
}