1

I've tried to get the patterns of all automation elements in that I can use ExpandCollapsePattern but i can't use the SelectionItemPattern to invoke SelectItemPattern.

Exception:

Exception: "Unsupported Pattern"

Here is my code:

        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);
                }
            }
        }
sid8491
  • 6,622
  • 6
  • 38
  • 64
KJay
  • 23
  • 6

1 Answers1

0

Afer you call pattern.Expand(); on a combo box it will then have a list with list item that you can get like the following image.

enter image description here

You want to get the list then get the list item you want and use the SelectionItemPattern on the child item. If the list is very long you will need to use the VirtualizedItemPattern to be created by calling Realize().

Here is some sudo code that hopefully helps.

    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);
            }
        }
    }

I am not sure how DevExpress has handles their child items but you may need to look around the visual tree for the combo boxes list. For instance we created our own combo box at my work and the control used to be implemented in such a way that the floating panel from a combo box belonged to the root of the application.

Max Young
  • 1,522
  • 1
  • 16
  • 42
  • I have try to foreach the listItem but it can only get a first item of dropdown list from combobox. In addition, I found an other solution on DevExpress support forum (https://www.devexpress.com/Support/Center/Question/Details/T458709/comboboxedit-wpf-automation). They use SetValuePattern to set a selected item value. However, I want to selected ComboBox item by index but it doesn't work. – KJay Jan 07 '18 at 07:55
  • Thank you for your help. – KJay Jan 07 '18 at 08:04