在GridViewRowPresenter(ListView)中将AutomationId分配给ContentPresenter [英] Assign an AutomationId to ContentPresenter in a GridViewRowPresenter (ListView)
问题描述
我正在尝试使用具有清单项列表的ListView进行编码的UI测试。
I'm trying to do coded UI testing with a ListView that is has a list of checkbox items.
由于编码的UI代码选择该复选框有麻烦单元格,我一直在尝试将AutomationId添加到控件中,以便进行编码的UI测试。
Due to trouble with the coded UI code selecting the checkbox cell, I have been trying to add AutomationId to the controls, so that the coded UI test works.
我快到了,在探听中我可以看到UIItemCell没有设置AutomationId,但是我不知道如何在我的应用程序中设置它。
I'm almost there, in snoop I can see that the UIItemCell does not have AutomationId set, but I can't figure out how to set it in my app.
UIItemCell是我需要设置AutomationId的位置
The UIItemCell is where I need to set AutomationId
我在Snoop上发现它是ContentPresenter
I found with Snoop that it's the ContentPresenter
ListView代码非常复杂,因此我会对其进行提炼
The ListView code is this complex, so I'll distill it a bit
<ListView HorizontalAlignment="Left"
Height="194"
Margin="53,123,0,0"
VerticalAlignment="Top"
Width="424"
AutomationProperties.AutomationId="listviewoption">
<ListView.Resources>
<Style x:Key="ListViewItemContainerStyle1" TargetType="{x:Type ListViewItem}">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Description }" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
AutomationProperties.AutomationId="Bxaid1" >
<Grid AutomationProperties.AutomationId="Gxaid1">
<!-- This is used when GridView is put inside the ListView -->
<GridViewRowPresenter AutomationProperties.AutomationId="gvrp"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<!-- ... -->
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
<ListView.ItemContainerStyle>
<StaticResource ResourceKey="ListViewItemContainerStyle1"/>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView AutomationProperties.AutomationId="aid1">
<GridViewColumn AutomationProperties.AutomationId="xc0"
DisplayMemberBinding="{Binding OptionName, Converter={StaticResource CamelCaseConverter}, Mode=OneWay}"
Width="180"/>
<GridViewColumn AutomationProperties.AutomationId="xc1"
Width="60">
<GridViewRowPresenter AutomationProperties.AutomationId="pp" />
<GridViewColumn.CellTemplate>
<DataTemplate >
<CheckBox Name="x1"
AutomationProperties.AutomationId="xaid1"
IsHitTestVisible="False"
HorizontalAlignment="Right"
Tag="{Binding OptionName}"
IsChecked=""
Padding="0"
Margin="0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
其中有些AutomationId并没有帮助,但它们是很好的参考点; gvrp是GridViewRowPresenter [016],其中包含我要放置ID的Content Presenter [017], xaid1是Content Presenter [017]内部的CheckBox。
There are some AutomationIds in there that haven't helped but are good points of reference; 'gvrp' is the GridViewRowPresenter [016] that holds the Content Presenter [017] that I want to put the id on, and 'xaid1' is the CheckBox inside the Content Presenter [017].
请在我爆头之前提供帮助。
Please help before my head explodes.
推荐答案
我最终可以通过
<GridViewRowPresenter Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<GridViewRowPresenter.Resources>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="AutomationProperties.AutomationId"
Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Name }"/>
</Style>
</GridViewRowPresenter.Resources>
</GridViewRowPresenter>
但是,自动测试生成的代码(编码的UI)仍然引用了表列(即使
However, the automatically testing generated code (coded UI) still referenced the table column (even though it was redundant) which was the problem I was trying to avoid in the first place...
无论如何,可以设置 AutomationId
ContentPresenter
中的code>,如果它对未来的人们有帮助,就在这里!
Anyway, it is possible to set the AutomationId
in the ContentPresenter
and in case it's helpful to anyone living in the future, here it is!
这篇关于在GridViewRowPresenter(ListView)中将AutomationId分配给ContentPresenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!