如何将 AutomationId 属性 正确设置为 ItemsControl?

How to set AutomationId property to ItemsControl correctly?

我遇到了 ui-WPF 中的自动化问题。我在测试中找不到通过查询控制的项目

rootElement.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "AID_Positions")))

它始终 return 为空。在界面检查器中,我看到 属性 已满并且具有正确的值 "AID_Positions".

我的xaml

       <ItemsControl
            Grid.Row="1"
            Grid.Column="1"
            VirtualizingStackPanel.IsVirtualizing="True"
            ScrollViewer.CanContentScroll="True">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    // data
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.Template>
                <ControlTemplate>
                    <Border
                        SnapsToDevicePixels="True">
                        <ScrollViewer
                           AutomationProperties.AutomationId="AID_Positions">
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
        </ItemsControl>

我将 'AutomationProperties.AutomationId' 属性 设置为 ScrollViewer。 我做错了什么?

尝试使用 UIA verify tool 检查您的 WPF UI 并查看该工具是否可以导航到您的 ItemsControl。您的 ItemsControl 可能不是 rootElement 的子树。该工具应该可以帮助您验证这一点。也可能存在一些不支持 UIA 且不允许查找其后代元素的中间自定义 wpf 元素。