UI Automation-GridPattern

GridPattern控件模式用于支持可充当子元素集合的容器的控件。该元素的子级必须实现IGridItemProvider,而且必须在可以按行和列进行遍历的二维逻辑坐标系中进行组织。支持GridPattern的最常见的控件为GridView,在WPF中使用ListViewGridView组合即可得到相应的GridView

GridPattern的方法:GetItem

此方法有两个参数,即DataGridRowColumn。通过GridPatternGetItem方法可以获取DataGrid的某个确定的单元格,进而对单元进行操作。对单元格的操作主要有以下几个方面:

·         编辑单元格中的数据;

·         获取单元格中的数据;

·         获取单元格中嵌套的AutomationElement(一般用于自定义控件中

GridPattern的属性

GridPatternCurrent属性中有如下两个属性:

·         RowCount属性:GridPattern二维表格的行数。

·         ColumnCount属性:GridPattern二维表格列数。

示例:

       public static void TestGridPattern()

        {

            Process adressbook = Process.Start(@"C:/Program Files/Outlook Express/wab.exe");

            Thread.Sleep(2000);

 

            //Get main window   "Address Book - Main Identity"

 

            AutomationElement addresswindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address Book - Main Identity"));

            Thread.Sleep(2000);

 

            //Get data grid "9001"

 

            AutomationElement dategrid=addresswindow.FindFirst(TreeScope.Descendants,new PropertyCondition(AutomationElement.AutomationIdProperty,"9001"));

            Thread.Sleep(2000);

 

            GridPattern dategridpattern=Utility.UIA.ControlPattern.Gridpattern.GetGridPattern(dategrid);

            AutomationElement item1 = dategridpattern.GetItem(2, 0);

            Console.WriteLine("cell which row='{0}',column='{1}',cell value is '{2}'", 2, 0, item1.Current.Name);

            Console.WriteLine("Grid row count='{0}',column count='{1}'",dategridpattern.Current.RowCount,dategridpattern.Current.ColumnCount);

 

 

        }

 

public class Gridpattern

    {

        public static GridPattern GetGridPattern(AutomationElement element)

        {

            object currentPattern;

            if (!element.TryGetCurrentPattern(GridPattern.Pattern, out currentPattern))

            {

                throw new Exception(string.Format("Element with AutomationID '{0} and Name '{1}' does not support the GridPattern.", element.Current.AutomationId, element.Current.Name));

            }

            return currentPattern as GridPattern;

        }

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据UIAutomation封装了很多自定义方法 现在只需要实例化之后 直接调用方法即可完成。比如单击某个按钮,现在只需要直接调用ClickElement,非常实用。 ClickElement 单击指定的自动化元素 DisselectAllDataGridRow 不选中所有行 DisselectDataGridRow 不选中特定的某一行 FocusWindow 获取窗口焦点 GetAllElement 获取指定父自动化元素下的所有激活的控件 GetAllElementDetails 获取指定自动化元素下的详细信息包括AutomationID,ControlType以及Name GetAllMenus 获取所有菜单项 GetAllSubMenus 获取某个菜单下的所有子菜单项 GetColumnsFromGridLine 获取指定行的所有列 GetColumnValuesFromGridLine 获取行的每一列数据 GetControlType(AutomationElement) 获取制动自动化元素的控件类型 GetControlType(TypeOfControl) 获取UIAutomation的控件类型 GetDocumentText 获取document控件的值 GetElementByID 获取父自动化元素下指定元素控件ID的引用 GetElementByName 获取父自动化元素下的指定子元素的引用 GetElementsByControlType 获取父自动化元素下的特定类型的所有自动化元素 GetGridLinesFromDataGrid 获取网格控件的全部行元素的引用 GetHeaderFromDataGrid 获取指定网格控件的标题栏引用 GetMenuBar 获取菜单栏控件 GetMenuByName 通过特定的名称去获取菜单UI自动化元素 GetName 获取指定自动化元素的名称 GetSubMenuByName 获取主菜单下的指定子菜单项的引用 GetValue 获取指定自动化元素的值 GetWindowByName(String) 获取desktop下的指定窗口名称的子UI自动化元素 GetWindowByName(String, AutomationElement) 获取特定父UI自动化元素下的制定窗口名称的子UI自动化元素 GetWindowList() 获取当前桌面根下所有的UI自动化元素下 GetWindowList(AutomationElement) 获取特定父UI自动化元素下的所有窗口的名称 RefindMainApplication 重新获取desktop下的指定窗口的自动化元素引用 SelectAllDataGridRow 选中所有行 SelectDataGridRow(AutomationElement) 选中特定的某一行 SelectDataGridRow(AutomationElement, Boolean) 将特定的DateGridRow加入选中项中 SelectValueInComboBox 从下拉框中选中指定值的项 SelectValueInListBox 从列表中选中指定值的项 SetValue 给予指定自动化元素赋值 以上的方法还不是很完善 正在完善中。如果有什么意见和建议,请发送邮件获取 chenxu7601257@qq.com 如果你看了这个帮助文件之后觉得有用的,请发邮件获取,我将把dll文件给你。谢谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值