2013-02-14 8 views
1

我有一个对象树,它有一个父表内的行对象。我试图把所有这些行成AutomationElementCollection使用Windows.Automation,我可以通过正则表达式找到AutomationElement吗?

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

的所有行AutomationElement.NameProperty包含字符串‘行’。然而,它们是该字符串的变体 - 例如“ROW1”,“行2”,“TopRow”,...

好像我可能失去了一些东西,因为FindAll方法允许你定义TreeScope和发现任何AutomationElement,其提供的Condition参数相匹配。我只是希望我的病情不受限制,因为我已经可以通过TreeScope控制查找范围。

回答

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; 
} 
+0

FindDescendant好像没有定义 – 2016-04-13 16:36:39

+0

@John Smith,我更新了答案,名字改名为FindFirstDescendant。 – 2016-04-13 16:41:05

3

As the documentation states,你可以要求一个不区分大小写的比较。没有“正则表达式”标志。你将不得不手动进行过滤。

+0

我认为这可能是这种情况 - 谢谢澄清! – Zee 2013-02-15 14:31:14