在尝试了几种解决方案后,我迫切需要帮助。
我尝试了几种方法,最后复制并仍然坚持使用使用 UIAutomation 获取 Datagrid 的完整内容的解决方案。
让我们谈谈代码,请考虑评论:
// Get Process ID for desired window handle
uint processID = 0;
GetWindowThreadProcessId(hwnd, out processID);
var desktop = AutomationElement.RootElement;
// Find AutomationElement for the App's window
var bw = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, (int)processID));
// Find the DataGridView in question
var datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "dgvControlProperties"));
// Find all rows from the DataGridView
var loginLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
// Badumm Tzzz: loginLines has 0 items, foreach is therefore not executed once
foreach (AutomationElement loginLine in loginLines)
{
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;
}
}
我既不能获取 a GridPattern
,也不能从中获取TablePattern
实例datagrid
,两者都会导致异常:
GridPattern gridPattern = null;
try
{
gridPattern = datagrid.GetCurrentPattern(GridPattern.Pattern) as GridPattern;
}
catch (InvalidOperationException ex)
{
// It fails!
}
TablePattern tablePattern = null;
try
{
tablePattern = datagrid.GetCurrentPattern(TablePattern.Pattern) as TablePattern;
}
catch (InvalidOperationException ex)
{
// It fails!
}
这些行是DataGridView
预先添加的,如下所示:
dgvControlProperties.Rows.Add(new object[] { false, "Some Text", "Some other text" });
我正在编译为 .Net Framework 4.5。尝试了 UI 自动化客户端的常规用户权限和提升的管理员权限,两者都产生了此处描述的相同结果。
为什么DataGridView
返回 0 行?
为什么我不能得到其中一种模式?
感谢您帮助我!
更新:
詹姆斯的帮助对我来说并没有成功。以下代码很难返回所有行(包括标题):
var rows = dataGrid.FindAll(TreeScope.Children, PropertyCondition.TrueCondition);
然后可以通过它们ControlType
的 of来识别标题单元格ControlType.Header
。