dotnet/wpf · error · InvalidOperationException

SR.UnsupportedPattern

Error message

SR.UnsupportedPattern

What it means

AutomationElement.GetCurrentPattern resolves a pattern object for the element via TryGetCurrentPattern; when the element does not support the requested pattern, the throwing wrapper raises InvalidOperationException with SR.UnsupportedPattern. It means the requested AutomationPattern is not currently supported by the element (or the provider did not expose it).

Solutions

  1. Use TryGetCurrentPattern and branch on its return value instead of GetCurrentPattern
  2. Check element support arrays (element.GetSupportedPatterns() or GetCurrentPropertyValue(AutomationElement.IsInvokePatternAvailableProperty)) before requesting
  3. Adjust the element lookup condition to target the right control type for the desired pattern

Example fix

// before
var invoke = (InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern);
// after
if (element.TryGetCurrentPattern(InvokePattern.Pattern, out var p))
{
    var invoke = (InvokePattern)p;
}
else
{
    // element does not support Invoke; pick an alternative pattern
}
Defensive patterns

Strategy: validation

Validate before calling

bool supported = (bool)element.GetCurrentPropertyValue(AutomationElement.IsInvokePatternAvailableProperty);
if (supported) { /* safe to GetCurrentPattern(InvokePattern.Pattern) */ }

Try / catch

try { var p = element.GetCurrentPattern(pat); }
catch (InvalidOperationException) { UseAlternativeInteraction(element); }

Prevention

When it happens

Trigger: Calling element.GetCurrentPattern(somePattern) where TryGetCurrentPattern returns false, e.g. requesting InvokePattern on a read-only text element, or requesting a pattern from a provider that only implements it conditionally.

Common situations: Assuming all controls support a pattern (buttons vs. text blocks); patterns disappearing dynamically when a control is disabled or changes state; mapping UI element types to patterns incorrectly in test frameworks.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b3b5329be2e24717. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs:550

        /// <summary>
        /// Get a pattern class from this object
        /// </summary>
        /// <param name="pattern">AutomationPattern indicating the pattern to return</param>
        /// <returns>Returns the pattern as an object, if currently supported</returns>
        /// <remarks>
        /// Throws InvalidOperationException if the pattern is not supported.
        /// 
        /// This API gets the pattern based on availability at this point in time,
        /// without checking the cache. For some types of UI, this API will incur
        /// a cross-process performance hit. To access patterns in this AutomationElement's
        /// cache, use GetCachedPattern instead.
        /// </remarks>
        public object GetCurrentPattern(AutomationPattern pattern)
        {
            object retObject;
            if (!TryGetCurrentPattern(pattern, out retObject))
            {
                throw new InvalidOperationException(SR.UnsupportedPattern);
            }

            return retObject;
        }


        /// <summary>
        /// Get a pattern class from this object
        /// </summary>
        /// <param name="pattern">an object repressenting the AutomationPattern indicating
        /// the pattern to return</param>
        /// <param name="patternObject">the returned pattern object will be an object 
        /// implementing the control pattern interface if the pattern is supported else 
        /// the object will be null.</param>
        /// <returns>Returns true, if currently supported</returns>
        /// <remarks>
        /// This API gets the pattern based on availability at this point in time,
        /// without checking the cache. For some types of UI, this API will incur

View on GitHub (pinned to 81131a70a4)