【问题标题】:UIAutomation won't return value of a children elementUIAutomation 不会返回子元素的值
【发布时间】:2017-04-04 04:33:38
【问题描述】:

以下是我在检查工具中的控制视图,我正在尝试从树下的文本块返回值:

Control in Inspect tool

在我的 WPF windows 应用程序中,数据网格有行和列,并试图获取特定行和列的文本值。第一行的文本值(时间戳)作为子树下的文本块。

检查控件属性有:

数据网格 - 数据网格

日志 - DataItemControlType

项目 - UIA_CustomControlType (DataGridCell)

自定义 - UIA_CustomControlType (DataGridCell)

文本 - UIA_TextControlTypeID

我可以得到总行数但无法得到文本值。

var gridChilds = grid.LowLevelAutomationObject.FindAll(System.Windows.Automation.TreeScope.Children, Condition.TrueCondition).OfType<AutomationElement>();
var rows = gridChilds.Count() ;

List<AutomationElement> messages = new List<AutomationElement>();
            TreeWalker walker = new TreeWalker(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
             AutomationElement ae = grdControl as AutomationElement;
            AutomationElement row = walker.GetFirstChild(ae);

                messages.Add(row);
            row = walker.GetNextSibling(row);
            string result = Convert.ToString(row);
            return result ;

我从在线帮助中尝试了一些类似问题的解决方案,但没有一个适用于我的示例。我是 C# 编码和工具的新手,因此我们将不胜感激。

【问题讨论】:

    标签: c# wpfdatagrid ui-automation textblock


    【解决方案1】:

    从检查的屏幕截图来看,您似乎想要单元格的 name 属性。所以代码看起来像这样。我从您的网格控件开始,我还假设行的类名是“Record”,因为网格中的大多数行都有一个类名“Record”。

    AutomationElement dataGrid = grdControl as AutomationElement;
    AutomationElement outerRow = dataGrid.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Record"));
    AutomationElement innerRow = outerRow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Record"));
    AutomationElement custom = innerRow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "custom"));
    AutomationElement text = custom.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "text"));
    
    return text.Name;
    

    【讨论】:

    • Max Young,非常感谢,我能够找到与您提供的上述解决方案类似的工作。在自定义级别循环并根据我给出的行号,它应该得到文本值,这将是“很好的”。但是非常感谢您抽出宝贵的时间:)
    • 没问题,如果您在 UI 自动化方面需要任何帮助,请随时给我发消息。用可用的资源来学习 UI 自动化是很可怕的。
    • Max Young,有没有办法可以将我的代码发送到您的收件箱消息中以解决不同的问题。不确定 SO 是否有能力向用户发布私人消息,因为我不想公开发布我的代码。请告诉我。
    • @cherryS 你可以在 gitter 上找到我的。请在gitter.im/FlaUI/Lobby 中给我发消息。
    猜你喜欢
    • 2020-10-13
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2021-06-14
    • 2013-01-05
    • 1970-01-01
    • 2010-11-12
    相关资源
    最近更新 更多