Using Windows.Automation, can I locate an AutomationElement by regex?

血红的双手。 提交于 2019-12-23 02:42:22

问题


I have an object tree that has row objects within a table parent. I'm attempting to put all these rows into an AutomationElementCollection

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

All of the rows' AutomationElement.NameProperty contains the string "row". However, they are variations of that string - e.g. "Row1", "Row2", "TopRow", ...

It seems like I may be missing something since the FindAll method allows you to define the TreeScope and find any AutomationElement, which matches the provided Condition parameter. I just want my condition to be unrestricted since I can already control the find scope by TreeScope.


回答1:


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



回答2:


As the documentation states, you can ask for a case-insensitive comparison. There is no "regular expression" flag. You will have to do the filtering manually.



来源:https://stackoverflow.com/questions/14884173/using-windows-automation-can-i-locate-an-automationelement-by-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!