0

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. 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. They are dynamically generated during runtime of my application. Only the grid that contains the data items is defined in the XAML. I will post a snippet of my XAML below.

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. There I'm stuck. I don't know how to set the automationIds for every AutomationElement there.

To sum it up I am either looking for:

  • Setting the automationID for data items in a WPF grid through XAML
  • Setting the automationID for data item AutomationElement through Code

Screenshot of my Inspect Tool: Inspect Tool

Snippet of my XAML Code:

    <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>
Mark
  • 2,392
  • 4
  • 21
  • 42
Turbine
  • 15
  • 3

2 Answers2

0

Try to specify ItemTemplate explicitly, it will let you set ids. It should be an automation API to iterate though ListView children as well.

0

Thanks @Viktor Syromiatnikov this got me on the right track. I now managed to set a static automationId for every ListViewItem.

Does anyone know how I can set the automationID dynamic for every ListViewItem so it looks like dataitem1, dataitem2, dataitem3 for every new ListViewItem?

My XAML looks like this now:

<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>
Turbine
  • 15
  • 3
  • I guess, to have a kind of dynamic value in `AutomationProperties.AutomationId` you could bind this property to the model items, but I doubt such approach fits nicely into MVVM :) – AntonK Nov 16 '22 at 09:19