使用UI Automation和.net的Datagrid内容

Chr*_*isO 5 .net c# datagrid ui-automation

我在使用UI自动化在外部应用程序中读取Datagrid的内容时遇到了一些麻烦,可能会使用一些指针.这是我到目前为止所拥有的:

int id = System.Diagnostics.Process.GetProcessesByName("Book")[0].Id;
AutomationElement desktop = AutomationElement.RootElement;

AutomationElement bw = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, id));

AutomationElement datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "lv"));

AutomationElementCollection lines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));

AutomationElementCollection items = lines[1].FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

GridItemPattern pattern = items[1].GetCurrentPattern(GridItemPattern.Pattern) as GridItemPattern;
TableItemPattern tablePattern = items[1].GetCurrentPattern(TableItemPattern.Pattern) as TableItemPattern;
Run Code Online (Sandbox Code Playgroud)

这可以在我可以从GridItemPattern和TableItemPattern访问列ID和行ID,但是如何访问该特定单元格中的值?它甚至可能吗?

谢谢.

Chr*_*isO 1

我终于弄清楚了,它需要使用 CacheRequest 来请求 AutomationElement 上的 Name 属性。这是最终的代码:

var cacheRequest = new CacheRequest
{
    AutomationElementMode = AutomationElementMode.None,
    TreeFilter = Automation.RawViewCondition
};

cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(AutomationElement.AutomationIdProperty);

cacheRequest.Push();

var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));

cacheRequest.Pop();

var myString = targetText.Cached.Name;
Run Code Online (Sandbox Code Playgroud)