UIAutomation to open ComboBox from Code

In WPF there’s an Automation Framework called UIAutomation. This Framework can be used to control UIElements from code. This can be useful for different scenarios:

  • Provide the user additional help
  • Control a programm by another programm
  • Test applications
  • Execute a user-action from code

The Automation Frameowork isn’t limited to WPF. You can also control Win32-Applications.

In WPF for every FrameworkElement you find a class named like [TheElementsName]AutomationPeer. E.g. for the Button-class there’s a ButtonAutomationPeer-class, for ComboBox there’s a ComboBoxAutomationPeer-class…

Such AutomationPeer-Classes support different Patterns. E.g. the ComboBoxAutomationPeer-class supports the ExpandCollapse-Pattern to expand and collapse the ComboBox. You do this with the following snippet:

ComboBoxAutomationPeer peer =
  new ComboBoxAutomationPeer(yourComboBox);
IExpandCollapseProvider provider = 
  (IExpandCollapseProvider)
  peer.GetPattern(PatternInterface.ExpandCollapse);
provider.Expand();

There’s much more you can do with UIAutomation, which is part of my German WPF-book (Chapter 19).

Share this post

Comments (3)

  • Daniel brown Reply

    Hi,

    Is that book in English as well? I was looking specifically for information about combo boxes and why I was finding it hard to select an element in the list through WPF UI automation. It appears that your book could save loads of time in the future, if the book is in English.

    Anyway thank you for this page as it has enlightening my path.

    Dan.

    July 22, 2010 at 2:22 am
  • hubethom Reply

    Hi Daniel,

    no, the book is just available in German. Sorry.

    Thomas

    August 2, 2010 at 3:49 pm
  • george Reply

    hi, daniel,
    I came across with the same problem as you, i can’t select an element in the list through WPF UI automation. Neither ComboBox.Select(int index) nor ComboBox.Select(string ItemName) can help. I wonder if you have a solution now. Plz mail me.

    December 15, 2010 at 8:50 am

Leave a Reply

Your email address will not be published. Required fields are marked *

*