使用Windows.Automation,我可以通过正则表达式找到AutomationElement吗?

时间:2013-02-14 21:14:59

标签: c# windows automation ui-automation automationelement

我有一个对象树,在父表中有行对象。我试图将所有这些行放入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来控制查找范围了。

2 个答案:

答案 0 :(得分:3)

As the documentation states,您可以要求不区分大小写的比较。没有“正则表达”标志。您必须手动进行过滤。

答案 1 :(得分:3)

//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;
}