【问题标题】:Setting automationIDs for dataitems in a WPF Grid for automation with UI Automation Framework使用 UI 自动化框架为 WPF 网格中的数据项设置自动化 ID 以实现自动化
【发布时间】:2021-03-18 16:28:27
【问题描述】:

我有一个使用 .NET Framework 3.5 用 C# 编写的应用程序,并希望使用 Microsoft UI 自动化框架自动化 UI。我不能在这里展示所有东西,因为它是我公司的软件。

在这个应用程序中,我正在生成一个显示我的数据项的网格。

我尝试通过 XAML 为网格中的每个数据项设置自动化 ID,但问题是,我看不到那里的数据项。它们是在我的应用程序运行时动态生成的。 XAML 中仅定义了包含数据项的网格。我将在下面发布我的 XAML 的 sn-p。

我也尝试通过程序的代码设置自动化ID,但我只设法获取每个数据项的AutomationElement。我被困住了。我不知道如何为那里的每个 AutomationElement 设置自动化 ID。

总而言之,我正在寻找:

  • 通过 XAML 为 WPF 网格中的数据项设置自动化 ID
  • 通过代码设置数据项 AutomationElement 的自动化 ID

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

我的 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>

【问题讨论】:

    标签: c# wpf xaml ui-automation microsoft-ui-automation


    【解决方案1】:

    尝试明确指定ItemTemplate,它会让你设置id。 它应该是一个自动化 API 来迭代 ListView 子项。

    【讨论】:

      【解决方案2】:

      感谢@Viktor Syromiatnikov 这让我走上了正轨。我现在设法为每个 ListViewItem 设置了一个静态自动化 ID。

      有谁知道我如何为每个 ListViewItem 动态设置自动化 ID,使其看起来像每个新 ListViewItem 的 dataitem1、dataitem2、dataitem3?

      我的 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        相关资源
        最近更新 更多