Search code examples
c#wpfautomationautomationelement

Sending clicks to an AutomationElement with no InvokePattern


Using AutomationElement, is there any way send clicks to a TabItem without having to move the mouse and simulate clicks? AutomationElement is still new to me - as far as I understand it, unless it supports the InvokePattern (which TabItem does not) you have to go the route of locating the control location and simulating the mouse. I have that code working (see below) - I am just curious if this is my option.

AutomationElement tabControl = GetControl(window, "NOTEBOOK");
AutomationElement tabGeneral = GetControl(tabControl, "FM_STAFF_SUB_P1");

AutomationElementCollection tabs = GetAllTabs(window, tabGeneral);

System.Windows.Point p = tabs[1].GetClickablePoint();

MoveMouse((int)p.X, (int)p.Y);
ClickMouse();

Thank you.


Solution

    1. Try tab.SetFocus()
    2. Get all supported patterns (tab.GetSupportedPatterns()), then see which ones are supported for this tab implementation. It should support SelectionItemPattern, so use: ((SelectionItemPattern)tab.GetCurrentPattern(SelectionItemPattern.Pattern)).Select()
    3. Use SendKeys to the window in order to navigate the tabs (most cases will have a hot key to navigate between them. You can combine by checking after each navigation if the tab is selected.
    4. If all the above fail, I guess mouse click is your only option.