【问题标题】:Getting selected value of UIAutomation Combo Box获取 UIAutomation 组合框的选定值
【发布时间】:2015-11-10 05:18:14
【问题描述】:

我正在尝试使用 UIAutomation 获取组合框的选定值。到目前为止我的代码如下:

var cbx = tree.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "desktopCbx"));
var value = cbx.Current.Name;

它没有给我正确的价值。我怎样才能找到合适的?

【问题讨论】:

  • UISpy 是否显示 cbx.Current.Name 中的文本?

标签: c# windows winforms ui-automation


【解决方案1】:

组合框AutomationElement 实现ValuePattern。您可以使用此模式设置或获取 ComboBox 的选定值。

我在我的框架中使用这些方法来设置和获取支持 ValuePattern 的 AutomationElement 的值:

public static string Element_GetValue(AutomationElement control)
{
    string value = null;

    object patternProvider;
    if (control.TryGetCurrentPattern(ValuePattern.Pattern, out patternProvider))
    {
        ValuePattern valuePatternProvider = patternProvider as ValuePattern;
        value = valuePatternProvider.Current.Value;
    }

    return value;
}

public static bool Element_SetValue(AutomationElement control, string value)
{
    object patternProvider;
    if (control.TryGetCurrentPattern(ValuePattern.Pattern, out patternProvider))
    {
        ValuePattern valuePatternProvider = patternProvider as ValuePattern;
        valuePatternProvider.SetValue(value);

        return true;
    }

    return false;
}

在您的场景中,您可以像这样使用Element_GetValue() 方法:

var cbx = tree.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "desktopCbx"));
var value = Element_GetValue(cbx);

【讨论】:

    【解决方案2】:

    通常 Combobox 是一个复合控件,包含一个编辑控件、一个按钮(用于下拉菜单)和一个列表。 所选项目文本通常是组合框的编辑控件子项的属性。

    UI Spy 应该很快就能解决这个问题。

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 2011-10-17
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多