2

我正在编写一个 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 间谍][1]

4

1 回答 1

3

Here is how you can do it:

        // 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();

To find these operations, you can use UISpy and try the different items in the tree, look at the pattern each item implements and try them out using the UISpy contextual "Control Patterns" menu.

于 2013-07-11T21:23:17.750 回答