dotnet/wpf · error · ArgumentOutOfRangeException

SR.ScrollViewer_CannotBeNaN

Error message

SR.ScrollViewer_CannotBeNaN

What it means

ScrollContentPresenter.ValidateInputOffset (invoked from scroll offset property-change callbacks) rejects NaN offsets with ArgumentOutOfRangeException because NaN would corrupt scrolling arithmetic (HorizontalOffset/VerticalOffset). Any non-NaN negative value is silently clamped to 0. The parameter name (offset or viewportWidth/Height context) is included in the message.

Solutions

  1. Validate the offset before passing it: replace NaN with 0 or a sensible default (double.IsNaN check).
  2. Fix the computation producing NaN (usually 0/0 or Infinity - Infinity) upstream.
  3. When persisting offsets, serialize 'not set' as 0 or null, never double.NaN.
  4. For bindings, use a value converter that maps NaN to 0, or FallbackValue on the binding.

Example fix

// before
scrollViewer.ScrollToVerticalOffset(ComputeOffset()); // may be NaN
// after
double off = ComputeOffset();
scrollViewer.ScrollToVerticalOffset(double.IsNaN(off) ? 0 : off);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(offset)) offset = 0; // or clamp before passing
offset = Math.Max(0.0, offset);

Type guard

bool IsValidScrollOffset(double v) => !double.IsNaN(v) && !double.IsInfinity(v);

Try / catch

try { scrollViewer.ScrollToVerticalOffset(offset); }
catch (ArgumentOutOfRangeException ex) when (ex.ActualValue is double d && double.IsNaN(d)) { offset = 0; }

Prevention

When it happens

Trigger: Setting ScrollViewer.HorizontalOffset/VerticalOffset, calling ScrollToHorizontalOffset/ScrollToVerticalOffset, or ScrollIntoView with double.NaN; data-binding an offset to a NaN-producing value (e.g. double division 0/0, unset double in code).

Common situations: Binding offsets to computed values that yield NaN (divide by zero on doubles), restoring persisted offsets where 'unset' was saved as NaN, custom scrolling logic passing Math-based NaN results.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/ScrollContentPresenter.cs:679

                return topChild;
            }

            // Handle Cases: 2 & 3 above
            else if (fAbove || fBelow || alignBottom)
            {
                alignBottom = true;
                return (bottomChild - (bottomView - topView));
            }

            // Handle cases: 5 & 6 above.
            return topView;
        }

        internal static double ValidateInputOffset(double offset, string parameterName)
        {
            if (double.IsNaN(offset))
            {
                throw new ArgumentOutOfRangeException(parameterName, SR.Format(SR.ScrollViewer_CannotBeNaN, parameterName));
            }
            return Math.Max(0.0, offset);
        }

        #endregion

        //-------------------------------------------------------------------
        //
        //  Private Methods
        //
        //-------------------------------------------------------------------

        #region Private Methods

        private ScrollData EnsureScrollData()
        {
            if (_scrollData == null) { _scrollData = new ScrollData(); }
            return _scrollData;

View on GitHub (pinned to 81131a70a4)