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).
Comments (3)
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.
Hi Daniel,
no, the book is just available in German. Sorry.
Thomas
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.