我正在编写一个 UI 自动化软件。我需要在数据网格中选择一行,然后单击运行按钮。我尝试了互联网上的大部分示例代码,但它们对我不起作用。例如选择 gridview 行:
当我编写以下代码时:
AutomationElement dataGrid = this.mainWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "2885"));
if (dataGrid != null)
{
GridPattern pattern = GetGridPattern(dataGrid);
AutomationElement tempElement = pattern.GetItem(1, 1);
tempElement.SetFocus();
}
我收到错误:“目标元素无法接收焦点。”这与最后一行相关。
我也尝试了代码:
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();
但是我收到了错误:“不支持的模式”。
我应该提到,我正在使用 UI Spy 来检索元素属性。
你能解释一下出了什么问题以及我应该如何选择一行吗? ![UI spy ][1]
最佳答案
以下是具体操作方法:
// get to ROW X (here it's row #1 name is always "Row X")
AutomationElement row1 = dataGrid.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Row 1"));
// get row header
AutomationElement row1Header = row1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header));
// invoke it (select the whole line)
((InvokePattern)row1Header.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
要找到这些操作,您可以使用 UISpy 并尝试树中的不同项目,查看每个项目实现的模式并使用 UISpy 上下文“控制模式”菜单尝试它们。
关于c# - 使用 UI 自动化选择数据网格的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578686/