dotnet/wpf · error · ArgumentOutOfRangeException

SR.Format(SR.ScrollViewer_OutOfRange, "verticalPercent"…

Error message

SR.Format(SR.ScrollViewer_OutOfRange, "verticalPercent", verticalPercent.ToString(CultureInfo.InvariantCulture), "0", "100")

What it means

SetScrollPercent validates its scroll percent argument and throws ArgumentOutOfRangeException when the value falls outside the accepted 0-100 range (SR.ScrollViewer_OutOfRange). The WPF automation layer requires percents expressed as a fraction of the total scrollable range, so any value below 0.0 or above 100.0 is rejected before the peer scrolls the owner ScrollViewer. This is a caller-input contract violation, not an internal failure.

Solutions

  1. Clamp or validate the percent to the 0-100 range before calling SetScrollPercent
  2. Compute the percent as offset/(extent-viewport)*100 rather than passing raw pixels
  3. If you intend 'no scrolling', pass null per the UIA contract instead of -1 where supported, or skip the call when the axis cannot scroll

Example fix

// before
peer.SetScrollPercent(150.0); // throws ArgumentOutOfRangeException
// after
double pct = Math.Clamp(150.0, 0.0, 100.0);
peer.SetScrollPercent(pct);
Defensive patterns

Strategy: validation

Validate before calling

if (percent < 0.0 || percent > 100.0)
    throw new ArgumentOutOfRangeException(nameof(percent), "Scroll percent must be 0-100");
peer.SetScrollPercent(percent);

Type guard

static bool IsValidScrollPercent(double v) => v >= 0.0 && v <= 100.0;

Try / catch

try { peer.SetScrollPercent(p); }
catch (ArgumentOutOfRangeException ex) { /* clamp or log: ex.ParamName, ex.ActualValue */ }

Prevention

When it happens

Trigger: Calling ScrollViewerAutomationPeer's IScrollProvider.SetScrollPercent (via UIA clients or directly) with verticalPercent or horizontalPercent < 0.0 or > 100.0 when the content scrolls in that axis; e.g. SetScrollPercent(150.0) or SetScrollPercent(-5). Note the source's && || precedence means the range check only runs when the axis actually scrolls.

Common situations: UIA test clients computing percent from pixel offsets and mis-scaling (forgetting the 0.01 factor or using viewport instead of extent); passing special UIA values like -1 (NoScroll) which this implementation does not special-case; scripts written for other frameworks that accept 0-1 fractions instead of 0-100 percents.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ScrollViewerAutomationPeer.cs:157

                throw new ElementNotEnabledException();

            bool scrollHorizontally = (horizontalPercent != (double)ScrollPatternIdentifiers.NoScroll);
            bool scrollVertically = (verticalPercent != (double)ScrollPatternIdentifiers.NoScroll);

            ScrollViewer owner = (ScrollViewer)Owner;

            if (scrollHorizontally && !HorizontallyScrollable || scrollVertically && !VerticallyScrollable)
            {
                throw new InvalidOperationException(SR.UIA_OperationCannotBePerformed);
            }

            if (scrollHorizontally && (horizontalPercent < 0.0) || (horizontalPercent > 100.0))
            {
                throw new ArgumentOutOfRangeException(nameof(horizontalPercent), SR.Format(SR.ScrollViewer_OutOfRange, "horizontalPercent", horizontalPercent.ToString(CultureInfo.InvariantCulture), "0", "100"));
            }
            if (scrollVertically && (verticalPercent < 0.0) || (verticalPercent > 100.0))
            {
                throw new ArgumentOutOfRangeException(nameof(verticalPercent), SR.Format(SR.ScrollViewer_OutOfRange, "verticalPercent", verticalPercent.ToString(CultureInfo.InvariantCulture), "0", "100"));
            }

            if (scrollHorizontally)
            {
                owner.ScrollToHorizontalOffset((owner.ExtentWidth - owner.ViewportWidth) * (double)horizontalPercent * 0.01);
            }
            if (scrollVertically)
            {
                owner.ScrollToVerticalOffset((owner.ExtentHeight - owner.ViewportHeight) * (double)verticalPercent * 0.01);
            }
        }

        /// <summary>
        /// Get the current horizontal scroll position
        /// </summary>
        /// <see cref="IScrollProvider.HorizontalScrollPercent"/>
        double IScrollProvider.HorizontalScrollPercent
        {

View on GitHub (pinned to 81131a70a4)