Windows UI自动化组合框的选择项

本文关键字:选择 组合 UI 自动化 Windows | 更新日期: 2023-09-27 18:14:05

我试图根据Windows UI自动化API的值选择一个项目。

我有一个继承自UIAutomation.Element的类ComboBox

另外,我在这个combobox元素上有一个方法,应该可以用string来调用它来选择匹配的combobox-item

我试过以下方法:

public void SetSelectedItem(string itemName, ITimeout timeout = null)
{
    var comboboxItem = this.GetSelf(timeout).FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
    var expandCollapsePattern = (ExpandCollapsePattern)this.GetSelf(timeout).GetCurrentPattern(ExpandCollapsePattern.Pattern);
    expandCollapsePattern.Expand();
    var itemToSelect = ?????
    var selectItemPattern = (SelectionItemPattern)itemToSelect.GetCurrentPattern(SelectionItemPattern.Pattern);
    selectItemPattern.Select();
}

但是我真的不知道如何在var itemToSelect = ?????行检索正确的项目。

变量comboboxItem的类型是AutomationElementCollection,但不幸的是,Linq似乎不能够与这种类型…

你知道如何检索正确的项目吗?

还是我做错了什么?

Thanks in advance

Windows UI自动化组合框的选择项

感谢@TnTinMn的提示,我找到了答案,谢谢!: -)

public void SetSelectedItem(string itemName, ITimeout timeout = null)
{
    this.GetSelf(timeout).Expand();
    var list = this.GetSelf(timeout).FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List), timeout);
    var listItem = list.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, itemName), timeout);
    listItem.Select();
}