如何使用 UIAutomation 在 WPF(DevExpress 框架)中 select ComboBoxItem?

How to select ComboBoxItem in WPF (DevExpress framework) using UIAutomation?

我试图获取所有自动化元素的模式,因为我可以使用 ExpandCollapsePattern 但我不能使用 SelectionItemPattern 来调用 SelectItemPattern.

异常:

Exception: "Unsupported Pattern"

这是我的代码:

        foreach (AutomationElement a in automationlist)
        {
            if (a.Current.AutomationId == "PersonalCountryCmb")
            {
                ExpandCollapsePattern pattern = (ExpandCollapsePattern)a.GetCurrentPattern(ExpandCollapsePattern.Pattern);
                pattern.Expand();
                try
                {
                    SelectionItemPattern pattern1 = (SelectionItemPattern)a.GetCurrentPattern(SelectionItemPattern.Pattern);
                    pattern1.Select();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
        }

在组合框上调用 pattern.Expand(); 后,它将有一个 listlist item,您可以得到如下图所示的结果。

您想要获得 list,然后获得您想要的 list item,并在子项上使用 SelectionItemPattern。如果列表很长,您将需要使用 VirtualizedItemPattern 通过调用 Realize() 创建。

这是一些希望对您有所帮助的 sudo 代码。

    foreach (AutomationElement a in automationlist)
    {
        if (a.Current.AutomationId == "PersonalCountryCmb")
        {
            ExpandCollapsePattern pattern = (ExpandCollapsePattern)a.GetCurrentPattern(ExpandCollapsePattern.Pattern);
            pattern.Expand();

            //Get list
            AutomationElement list = a.FindFirst(TreeScope.Children, new PropertyCondition(
    AutomationElement.LocalizedControlType, "list");

            //Get list item, you will need to replace the condition with something else here
            AutomationElement listItem = a.FindFirst(TreeScope.Children, new PropertyCondition(
    AutomationElement.LocalizedControlType, "list item");

            try
            {
                SelectionItemPattern pattern1 = (SelectionItemPattern)listItem.GetCurrentPattern(SelectionItemPattern.Pattern);
                pattern1.Select();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }
    }

我不确定 DevExpress 如何处理它们的子项,但您可能需要查看组合框列表的可视化树。例如,我们在工作中创建了自己的组合框,控件过去以组合框的浮动面板属于应用程序根的方式实现。