AutomationPeer, update UIA tree when children get removed

852 views Asked by At

(Beginner with UI Automation and WPF here, so perhaps I'm doing things very wrong.)

We have a custom control that displays items to be interactively viewed or edited. In adding UI Automation support I now used an appropriate implementation of OnCreateAutomationPeer and implemented an AutomationPeer specific for that control. Things seem to be working fine for the most part. I can see our children in the UIA tree and when I add children they appear there.

From what I've seen now in the .NET Source is that thr tree changes work automagically because each UpdateLayout from within the bowels of WPF causes the respective AutomationPeers to refresh their subtree. So those things work fine out of the box.

However, when removing an item my AutomationPeer's GetChildrenCore is not called, apparently because WPF doesn't really need to update anything when a Visual is removed from its tree. This leads to the UIA tree containing elements that are no longer there.

Now I wonder though, how this is supposed to work in general. I saw that some controls notify their AutomationPeers directly when something changes. This might be a route. Or should our custom control do something different so that layout happens even when something is removed?

1

There are 1 answers

1
John Templer On

I have had a similar issue with an ItemsControl - I update the contents dynamically and it does not refresh the children for use with automation.

In my user control I added code that was triggered on a viewmodel event, which then called ResetChildrenCache() on the automation peer. My Code looks like this:

viewModel.DisplayedFaults.CollectionChanged += (sender, args) =>
{
    automationPeer = UIElementAutomationPeer.FromElement(AutomationUiItemsControl);
    if (automationPeer != null)
    {
        automationPeer.ResetChildrenCache();
    }
};

It was based on this article https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/636a37a6-d766-46f8-bafb-d4d9a7816345/wpf-ui-automation-on-a-treeview?forum=windowsaccessibilityandautomation

Another Useful link I found related to this kind of thing is: Some controls on a page are not visible for MS UI Automation