如何为ItemsControl动态生成自动化ID [英] How to generate automation id dynamically for ItemsControl

查看:91
本文介绍了如何为ItemsControl动态生成自动化ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

我们的质量检查需要对WPF构建的应用程序进行自动化测试.自动化测试基于控件的自动化ID进行中继,例如:

Our QA needs do automation tests for the application which is built by WPF. The automation tests relay on automation ids of controls, for instance:

 

<StackPanel Margin="147,39,129,46" Name="stackPanel1">
      <Button AutomationProperties.AutomationId="button1">YY</Button>
      <Label AutomationProperties.AutomationId="Label3" Margin="0,2">Yangzheng</Label>
      <Button x:Name="button2" Margin="0,5">Super Yang</Button>
</StackPanel>
    <ListBox>
      <ListBox.ItemsPanel>
...
</ListBox.ItemsPanel>
<ListBox.ItemsSource> <Binding Source="{x:Static WpfClient:Client.Instance}" Path="IMManager.LocalIMs" BindsDirectlyToSource="True" Mode="OneWay"> </Binding> </ListBox.ItemsSource> </ListBox>

如上所示,可以通过"AutomationProperties.AutomationId"来设置堆栈面板内的控件的自动化ID.或"x:名称",可以由某些自动化工具检测到.

As shown above, controls within stackpanel can be set automation ids via "AutomationProperties.AutomationId" or "x:name", which can be detected by some automation tools.

但是对于该ListBox,由于其项源绑定到集合,如何为其动态生成自动化ID?

But for that ListBox, because its items source are binding to a collection, how to generate automation ids dynamically for it?

是否有一种优雅的方法来做到这一点?

Is there an elegant approach to do this?

 

非常感谢!

 

推荐答案

那么为ListBox中的每个项目生成的自动化ID是否与其包含的数据有某种关联?您能详细说明吗?

So is the automation ID generated for each item in the ListBox has some sort of association with its containing data or not? Could you elaborate?

我认为通过定义自己的项目模板并使用转换器可以解决该问题.但是仍然需要更多信息.

I think by defining your own item template and with a converter might solve the problem. But still need more information.

谢谢,

在论坛中 MSDN订阅者支持 如果您对我们的支持有任何反馈,请联系 msdnmg@microsoft.com

Thanks,
Jie
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


这篇关于如何为ItemsControl动态生成自动化ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆