Search code examples
c#user-interfacedesign-patternsmicrosoft-ui-automation

Getting actual InvokePattern object from a list of AutomationPattern objects


I have a list of AutomationPattern objects retrieved from the AutomationElement.GetSupportedPatterns() method. And now I need to know what kind of patterns are there, I mean is the single item InvokePattern or ExpandCollapsePattern. To be more specific let's assume we have an AutomationPattern object and we know that the ProgrammaticName property of this object is the "InvokePatternIdentifiers.Pattern" string, can I somehow get object of type InvokePattern?

I have implemented my own way of solving this problem using AutomationPattern.ProgrammaticName property and a big switch statement, is there another way to do this?

Here is my implemetation:

    public object GetActualAutomationPattern(AutomationElement element, AutomationPattern pattern)
    {
        switch (pattern.ProgrammaticName)
        {
            case "DockPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(DockPattern.Pattern) as DockPattern;
            case "ExpandCollapsePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
            case "GridPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(GridPattern.Pattern) as GridPattern;
            case "GridItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(GridItemPattern.Pattern) as GridItemPattern;
            case "InvokePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
            case "MultipleViewPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(MultipleViewPattern.Pattern) as MultipleViewPattern;
            case "RangeValuePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(RangeValuePattern.Pattern) as RangeValuePattern;
            case "ScrollPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
            case "ScrollItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ScrollItemPattern.Pattern) as ScrollItemPattern;
            case "SelectionPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
            case "SelectionItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
            case "TablePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TablePattern.Pattern) as TablePattern;
            case "TableItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TableItemPattern.Pattern) as TableItemPattern;
            case "TogglePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
            case "TransformPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TransformPattern.Pattern) as TransformPattern;
            case "ValuePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
            case "WindowPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
            default:
                return pattern;
        }
    }

Solution

  • You can just do something like:

    object patternObj = element.TryGetCurrentPattern(pattern);
    

    and then just test whether or not the type is what you want:

    if (patternObj != null && patternObj is InvokePattern) {...}