简体   繁体   English

在 WPF 网格中为数据项设置自动化 ID,以使用 UI 自动化框架实现自动化

[英]Setting automationIDs for dataitems in a WPF Grid for automation with UI Automation Framework

I've got an application that's written in C# using .NET Framework 3.5 and want to automate the UI using Microsoft UI Automation Framework.我有一个使用 .NET 框架 3.5 以 C# 编写的应用程序,并希望使用 Microsoft UI 自动化框架自动化 UI。 I'm not allowed to show everything here, because it's a software from my company.我不能在这里展示所有东西,因为它是我公司的软件。

In this application I'm generating a grid that displays my data items.在这个应用程序中,我正在生成一个显示我的数据项的网格。

I've tried to set the automationIDs for every data item in the grid through XAML, but the problem is, that I can't see the dataitems there.我尝试通过 XAML 为网格中的每个数据项设置自动化 ID,但问题是,我看不到那里的数据项。 They are dynamically generated during runtime of my application.它们是在我的应用程序运行时动态生成的。 Only the grid that contains the data items is defined in the XAML.在 XAML 中仅定义了包含数据项的网格。 I will post a snippet of my XAML below.我将在下面发布我的 XAML 的片段。

I've also tried to set the automationIDs through the Code of the program, but I only managed to get the AutomationElement of every data item.我也尝试通过程序代码设置自动化ID,但我只设法获取每个数据项的AutomationElement。 There I'm stuck.我被困住了。 I don't know how to set the automationIds for every AutomationElement there.我不知道如何为那里的每个 AutomationElement 设置自动化 ID。

To sum it up I am either looking for:总而言之,我正在寻找:

  • Setting the automationID for data items in a WPF grid through XAML通过 XAML 为 WPF 网格中的数据项设置自动化 ID
  • Setting the automationID for data item AutomationElement through Code通过代码设置数据项AutomationElement的automationID

Screenshot of my Inspect Tool: Inspect Tool我的检查工具的屏幕截图:检查工具

Snippet of my XAML Code:我的 XAML 代码片段:

    <ListView IsEnabled="{Binding TestSelectorEnabled}" x:Name="ListView1" ItemsSource="{Binding TestsViewModel.TestsDataTable}" SelectedValue="{Binding TestsViewModel.SelectedTest}" SelectedValuePath="TestName" Height="180" View="{Binding TestsViewModel.GridView}"  TabIndex="0" MinWidth="1" Focusable="False"/>
            <Grid  Visibility="Collapsed" IsSharedSizeScope="False">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="40"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="20"></RowDefinition>
                </Grid.RowDefinitions>
            </Grid>

Try to specify ItemTemplate explicitly, it will let you set ids.尝试明确指定ItemTemplate ,它会让你设置 id。 It should be an automation API to iterate though ListView children as well.它应该是一个自动化 API 来迭代 ListView 子项。

Thanks @Viktor Syromiatnikov this got me on the right track.谢谢@Viktor Syromiatnikov,这让我走上了正轨。 I now managed to set a static automationId for every ListViewItem.我现在设法为每个 ListViewItem 设置了 static automationId。

Does anyone know how I can set the automationID dynamic for every ListViewItem so it looks like dataitem1, dataitem2, dataitem3 for every new ListViewItem?有谁知道我如何为每个 ListViewItem 动态设置自动化 ID,以便每个新 ListViewItem 看起来像 dataitem1、dataitem2、dataitem3?

My XAML looks like this now:我的 XAML 现在看起来像这样:

<ListView IsEnabled="{Binding TestSelectorEnabled}" x:Name="ListView1" ItemsSource="{Binding TestsViewModel.TestsDataTable}" SelectedValue="{Binding TestsViewModel.SelectedTest}" SelectedValuePath="TestName" Height="180" View="{Binding TestsViewModel.GridView}"  TabIndex="0" MinWidth="1" Focusable="False">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="AutomationProperties.AutomationId" Value="dataitem"/>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM