我有一个对象树,它在表父级中有行对象。我正试图将所有这些行放入AutomationElementCollection

AutomationElementCollection asdf = ParentTableObj.FindAll
     (
     TreeScope.Children,
     new PropertyCondition
          (
          AutomationElement.NameProperty,
          "I want to use regex here"
          )
     );

所有行'AutomationElement.NameProperty都包含字符串“row”。但是,它们是该字符串的变体,例如“row1”、“row2”、“toprow”…
似乎我遗漏了一些东西,因为FindAll方法允许您定义TreeScope并查找任何与提供的AutomationElement参数匹配的Condition。我只希望我的条件不受限制,因为我已经可以通过TreeScope控制查找范围。

最佳答案

//Example :
AutomationElement element = FindFirstDescendant(
    AutomationElement.FromHandle(windows_hWnd),
    (ele)=>Regex.IsMatch( ele.Current.Name, pattern)
);

//The generic method to find a descendant element:
public static AutomationElement FindFirstDescendant(AutomationElement element, Func<AutomationElement, bool> condition) {
    var walker = TreeWalker.ControlViewWalker;
    element = walker.GetFirstChild(element);
    while (element != null) {
        if (condition(element))
            return element;
        var subElement = FindFirstDescendant(element, condition);
        if (subElement != null)
            return subElement;
        element = walker.GetNextSibling(element);
    }
    return null;
}

关于c# - 使用Windows.Automation,我可以通过regex找到AutomationElement吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14884173/

10-13 03:10