我需要从 White 访问 dataGrid 并且不知道为什么 White 不起作用(我有源代码,如果我有时间仔细研究它)但是,我已经编写了一些基本代码来将网格数据提取到大批。幸运的是,White 框架提供了对 AutomationElement 的访问。
下面的代码没有优化……它是在 LinqPad 中拼凑起来的!
// The first few lines use White
var application = Application.Attach("AppName");
var window = application.GetWindow("The Window Title");
var datagrid = window.Get<White.Core.UIItems.TableItems.Table>("dataGridAutomationId").AutomationElement;
// Now it's using UI Automation
var headerLine = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header));
var cacheRequest = new CacheRequest { AutomationElementMode = AutomationElementMode.Full, TreeScope = TreeScope.Children };
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(ValuePattern.Pattern);
cacheRequest.Push();
var gridLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
cacheRequest.Pop();
Console.WriteLine (headerLine.Count + " columns");
Console.WriteLine (gridLines.Count + " rows");
var gridData = new string[headerLine.Count, gridLines.Count];
var headerIndex = 0;
foreach (AutomationElement header in headerLine)
{
gridData[headerIndex++, 0] = header.Current.Name;
}
var rowIndex = 1;
foreach (AutomationElement row in gridLines)
{
foreach (AutomationElement col in row.CachedChildren)
{
// Marry up data with headers (for some reason the orders were different
// when viewing in something like UISpy so this makes sure it's correct
headerIndex = 0;
for (headerIndex = 0; headerIndex < headerLine.Count; headerIndex++)
{
if (gridData[headerIndex, 0] == col.Cached.Name)
break;
}
gridData[headerIndex, rowIndex] = (col.GetCachedPattern(ValuePattern.Pattern) as ValuePattern).Current.Value;
}
rowIndex++;
}