dotnet/wpf · error · ArgumentOutOfRangeException

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

Error message

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

What it means

ScrollViewerAutomationPeer.SetScrollPercent throws ArgumentOutOfRangeException with SR.ScrollViewer_OutOfRange when horizontalPercent is outside the valid 0-100 range (and not the NoScroll sentinel -1). Note the condition `scrollHorizontally && (horizontalPercent < 0.0) || (horizontalPercent > 100.0)` means a non-scrollable-axis check also reaches here with out-of-range values.

Solutions

  1. Convert ratios to percentages: multiply by 100 before calling
  2. Use ScrollPatternIdentifiers.NoScroll (-1) to skip an axis
  3. Clamp values to the 0-100 range before the call
  4. Catch ArgumentOutOfRangeException and log/normalize the input

Example fix

// before
scrollPattern.SetScrollPercent(0.75, ScrollPatternIdentifiers.NoScroll); // ratio!
// after
scrollPattern.SetScrollPercent(75.0, ScrollPatternIdentifiers.NoScroll); // percent
Defensive patterns

Strategy: validation

Validate before calling

if (h != ScrollPatternIdentifiers.NoScroll && (h < 0.0 || h > 100.0))
    throw new ArgumentException("horizontalPercent must be 0-100 or NoScroll");

Type guard

static bool IsValidPercent(double p) =>
    p == ScrollPatternIdentifiers.NoScroll || (p >= 0.0 && p <= 100.0);

Try / catch

try { sp.SetScrollPercent(h, v); }
catch (ArgumentOutOfRangeException e) { /* clamp/normalize and retry */ }

Prevention

When it happens

Trigger: Calling SetScrollPercent with horizontalPercent < 0 (other than NoScroll -1) or > 100 — e.g. normalized fractions like 0.5 or 1.0 intended as percent.

Common situations: Passing normalized scroll ratios (0..1) instead of percentages (0..100); COM clients sending NaN/odd values; NegativeGet off-by-one like 150.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

        /// <see cref="IScrollProvider.SetScrollPercent"/>
        void IScrollProvider.SetScrollPercent(double horizontalPercent, double verticalPercent)
        {
            if(!IsEnabled())
                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

View on GitHub (pinned to 81131a70a4)