TablePattern控件模式用于支持充当子元素集合的容器的控件。该元素的子级必须实现ITableItemProvider,而且必须在可以按行何列进行遍历的二维逻辑坐标系中进行组织。此控件模式类似于IgridProvider,不同之处在于任何实现ITableProvider的控件必须同时公开每个元素的列何或行标题关系。
RowOrColumnMajor,是检索表格首要遍历方向。
GetColumnHeader是获取一个表示表格中所有列标题的UI自动化提供程序的集合。
GetRowHeaders是检索表格中所有行标题的UI自动化提供程序的集合。
示例:
public static void TestTablePattern()
{
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
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);
//Test TablePattern
TablePattern tablepattern = Utility.UIA.ControlPattern.Tablepattern.GetTablePattern(dategrid);
AutomationElement table = tablepattern.GetItem(1, 1);
Console.WriteLine("table name='{0}'",table.Current.Name);
//Test TableItemPattern
TableItemPattern tableitempattern = Utility.UIA.ControlPattern.TableItempattern.GetTableItemPattern(item1);
Console.WriteLine("header='{0}'", tableitempattern.Current.Column.ToString());
}
public static TablePattern GetTablePattern(AutomationElement element)
{
object currentPattern;
if (!element.TryGetCurrentPattern(TablePattern.Pattern, out currentPattern))
{
throw new Exception(string.Format("Element with AutomationID '{0} and Name '{1}' does not support the TablePattern.", element.Current.AutomationId, element.Current.Name));
}
return currentPattern as TablePattern;
}