WPF UI自动化 - 不支持行? (或者如何选择和取消选择整行)

时间:2010-01-22 02:23:34

标签: wpf datagrid .net-3.5 ui-automation

注意:这不再是问题,.NET 4内置的DataGrid解决了这个问题


我有一个使用DataGrid的WPF应用程序;我正在使用WPF ui自动化API为它编写一些自动化测试。 DataGrid是WPFToolkit,我使用带有VS2008的.NET 3.5SP1,并且数据网格已启用多选。

我所处的位置是我的测试可以找到数据网格,我可以使用GridPattern.GetItem方法在网格中找到单个单元格,并通过设置SelectionItemPattern.Select来选择它们。方法

代码看起来有点像这样:

AutomationElement mainGrid = // find the grid in the window
var columnCount = (int)mainGrid.GetCurrentPropertyValue(GridPattern.ColumnCountProperty);
var mainGridPattern = (GridPattern)mainGrid.GetCurrentPattern(GridPattern.Pattern);

var rowToSelect = 2;

// select just the first cell
var item = mainGridPattern.GetItem(rowToSelect, 0);
var itemPattern = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern);
itemPattern.Select();

这似乎有效,但它只选择第一个单独的单元格,而不是整个表格行(有10列),但我无法弄清楚如何取消选择项目。我能找到的唯一可能是它可能有用的是在itemPattern或相应的RemoveFromSelection上调用SelectionItemPattern.AddToSelection()但是当我执行其中任何一项时,会引发以下异常:

=> Cannot change cell selection when the SelectionUnit is FullRow.
   at MS.Internal.Automation.ElementUtil.Invoke(AutomationPeer peer, DispatcherOperationCallback work, Object arg)
   at MS.Internal.Automation.SelectionItemProviderWrapper.AddToSelection()

根本问题似乎是(据我所知)WPF UI自动化API没有网格行的概念,只有单元格。这似乎有点问题 - 这是对的吗?

旁注:我以前一直使用白色UI自动化框架 - 这不使用UI自动化来选择网格行,而是将鼠标移动到行位置并单击它 - 这导致我们的测试随机失败 - 是这就是为什么他们使用鼠标进行选择呢?

2 个答案:

答案 0 :(得分:0)

如果您使用UISpy查看DataGrid的结构,UIAutomation会看到它,您会注意到DataGrid元素包含RowsPresenter,而RowsPresenter包含许多DataGridRows,每个DataGridRows包含一个DataGridCell。

我怀疑发生的事情是var item = mainGridPattern.GetItem(rowToSelect, 0);返回表示单元格的项目,而您希望项目代表整行。

您可以致电item.CachedParent - 然后Select()来发送此邮件。

答案 1 :(得分:0)

现在似乎已经在.NET 4中修复了。现在,行显示为支持SelectionItemPattern的真实对象,因此您现在可以选择/取消选择行。万岁

相关问题