dotnet/wpf · error · InvalidOperationException

SR.OperationCannotBePerformed

Error message

SR.OperationCannotBePerformed

What it means

WindowsScroll.SetScrollPercent first verifies the window actually has a scrollable bar via IsScrollable(hwnd); if not, it throws InvalidOperationException with SR.OperationCannotBePerformed. UIAutomation's Scroll pattern was invoked on a control that cannot be scrolled in the requested direction.

Solutions

  1. Check ScrollPattern.ScrollInfo / IsScrollable before calling SetScrollPercent and skip when not scrollable.
  2. Ensure the target control actually shows scroll bars (resize the window or populate more content) before automating.
  3. Catch InvalidOperationException and treat the element as non-scrollable instead of failing the automation run.

Example fix

// before
scrollPattern.SetScrollPercent(50, ScrollPattern.NoScroll);
// after
if (scrollPattern.Current.VerticallyScrollable)
    scrollPattern.SetScrollPercent(ScrollPattern.NoScroll, 50);
Defensive patterns

Strategy: validation

Validate before calling

var sp = (ScrollPattern)element.GetCurrentPattern(ScrollPattern.Pattern);
bool ok = sp.Current.HorizontallyScrollable || sp.Current.VerticallyScrollable;

Try / catch

try { sp.SetScrollPercent(h, v); }
catch (InvalidOperationException) { /* element is not scrollable; skip */ }

Prevention

When it happens

Trigger: Calling the Scroll pattern's SetScrollPercent (ScrollPattern.SetScrollPercent) on a window whose scroll bars are disabled or non-scrollable (IsScrollable returns false), detected at WindowsScroll.cs:31.

Common situations: Automating a list/view whose content currently fits without scrolling; scroll bars hidden because 'auto-hide scroll bars' is on; targeting a child control that has no scroll style bits (WS_HSCROLL/WS_VSCROLL).

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/9bd272a9368d0f8b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScroll.cs:31

namespace MS.Internal.AutomationProxies
{
    // Static class used to support the Scroll pattern for controls that have scroll bars. 
    internal static class WindowScroll
    {
        #region Internal Methods

        // ------------------------------------------------------
        //
        // Internal Methods
        //
        // ------------------------------------------------------

        // Request to scroll Horizontally and vertically by the specified amount
        internal static void SetScrollPercent (IntPtr hwnd, double horizontalPercent, double verticalPercent, bool forceResults)
        {
            if (!IsScrollable(hwnd))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

            bool resultsNoCheck;
            bool isHorizontal = SetScrollPercent (hwnd, horizontalPercent, NativeMethods.SB_HORZ, out resultsNoCheck);

            // This is needed for Controls that do not return the proper return codes to WinAPI Calls
            if (!isHorizontal && (forceResults && resultsNoCheck))
            {
                isHorizontal = true;
            }

            bool isVertical = SetScrollPercent (hwnd, verticalPercent, NativeMethods.SB_VERT, out resultsNoCheck);

            // This is needed for Controls that do not return the proper return codes to WinAPI Calls
            if (!isVertical && (forceResults && resultsNoCheck))
            {
                isVertical = true;
            }

View on GitHub (pinned to 81131a70a4)