使用UIAutomation获取Datagrid的完整内容

我需要使用UIAutomation从外部应用程序检索Datagrid中的所有项目。 目前,我只能检索(并在UISpy中查看)可见项目。 有没有办法缓存Datagrid中的所有项目然后拉它们? 这是代码:

static public ObservableCollection GetLogins() { ObservableCollection returnLogins = new ObservableCollection(); var id = System.Diagnostics.Process.GetProcessesByName("")[0].Id; var desktop = AutomationElement.RootElement; var bw = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, id)); var datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "lv")); var loginLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem)); foreach (AutomationElement loginLine in loginLines) { var loginInstance = new Login { IP = new IP() }; var loginLinesDetails = loginLine.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)); for (var i = 0; i < loginLinesDetails.Count; i++) { 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; #region Determine data and write to return object //Removed private information #endregion } } returnLogins.Add(loginInstance); } return returnLogins; } 

您只能检索可见单元格,因为您已启用表格虚拟化。

尝试禁用虚拟化(并非总是可以在所有应用程序中使用,但您可能希望将其移至配置中并在测试前进行更改)

我99%肯定这是不可能的。 UI自动化不了解由网格的当前可见部分表示的数据结构。 它只能看到可见的东西。 我认为您必须通过网格页面来获取所有数据(这就是我所做的)。