有时候我们会要捕捉一组有相同属性的控件就要用到AutomationElementCollection 类。
AutomationElementCollection 类
表示 AutomationElement 对象的集合
比如我们要捕捉Calculator上的所有Button.
- using System.Windows.Automation;
- ....
- //找到Desktop
- AutomationElement Desktop = AutomationElement.RootElement;
- //找到Calculator窗口
- AutomationElement CalcWindows = Desktop.FindFirst(TreeScope.Children,
- new AndCondition(
- new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
- new PropertyCondition(AutomationElement.ClassNameProperty, "SciCalc")
- )
- );
- //找到全部在Calculator子自动化树下的Button控件
- AutomationElementCollection Buttons = CalcWindows.FindAll(TreeScope.Children,
- new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)
- );
AutomationElementCollection主要是通过AutomationElement.FindAll方法获得,
AutomationElement.FindAll的语法和AutomationElement.FindFirst一样.
public AutomationElementCollection FindAll( TreeScope scope, Condition condition )
转载来源:http://blog.csdn.net/vbic0673/article/details/6093844