Announce changes with ItemStatus WPF

After announcing changes on the screen with LifeRegionChanged, I found how I can use ItemStatus to do something similar for the focused element. Using ItemStatus can be used to keep track of the status of an element and announce status changes on the element. In this post, I’ll give a sample of validation on a TextBox and explain how to use AccEvent.exe tool to see the accessibility events.

When having validation on a textbox and you want to validate the text in the TextBox while typing, AutomationPoperties.ItemStatus can be a good tool to do this. However, you have to do a little more as just changing the ItemStatus binding to the validation. When only setting the ItemStatus with a data binding, the status is only announced when the TextBox is refocused.

>TextBox x:Name="demoLinkBox"
                AutomationProperties.Name="{x:Static local:Resources.demoLinkBoxText}"
                AutomationProperties.LabeledBy="{Binding ElementName=demoLinkBoxLabel}"
                AutomationProperties.ItemStatus="{Binding [0].ErrorContent}"/<

To find out what happens on the background you can have a look at the UI automation events with accevent.exe. If the RaisePropertyChangedEvent for the AutomationElementIdentifiers.ItemStatusProperty is not triggered, you can do this by hand:

void ValidationChange(string newStatus)
{
    // Get the current UIA ItemStatus from the header element. 
    var oldStatus = AutomationProperties.GetItemStatus(demoTextBox);

    // Set some sample new ItemStatus here... 
    AutomationProperties.SetItemStatus(demoTextBox, newStatus);

    // Having just set the new ItemStatus, raise a UIA property changed event. Note that the peer may 
    // be null here unless a UIA client app such as Narrator or the AccEvent SDK tool are running. 
    var peer = FrameAutomationPeer.FromElement(demoTextBox);
    peer?.RaisePropertyChangedEvent(AutomationElementIdentifiers.ItemStatusProperty, oldStatus, newStatus);
}

When you now attach the narrator, the new status will be read while typing.

Comparing ItemStatus and LifeRegionChanged they both have their uses. The ItemStatus can be used when an element on the screen has focus. The LifeRegionChanged can be used when something (a status) or a region on the screen changed.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.