dotnet/wpf · error · ArgumentOutOfRangeException

SR.ArgumentOutOfRange_Generic_MustBeFinite

Error message

SR.ArgumentOutOfRange_Generic_MustBeFinite

What it means

TextBoxBase.ScrollToHorizontalOffset validates that the offset is a finite number; a NaN offset triggers ArgumentOutOfRangeException (the SR string for 'must be a finite number'). NaN would propagate into ScrollViewer layout math and corrupt scrolling state, so the API rejects it up front.

Solutions

  1. Check Double.IsNaN(offset) before calling and skip or clamp the call
  2. Pass 0 or a computed finite offset instead of NaN
  3. Ensure bound values are computed after layout (e.g. in Loaded or via Dispatcher.BeginInvoke) so ActualWidth-based math is finite

Example fix

// before
textBox.ScrollToHorizontalOffset(computedOffset); // may be NaN
// after
if (!double.IsNaN(computedOffset))
    textBox.ScrollToHorizontalOffset(computedOffset);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(offset) || double.IsInfinity(offset))
    return;

Type guard

static bool IsFinite(double d) => !double.IsNaN(d) && !double.IsInfinity(d);

Try / catch

try
{
    textBox.ScrollToHorizontalOffset(offset);
}
catch (ArgumentOutOfRangeException)
{
    textBox.ScrollToHorizontalOffset(0);
}

Prevention

When it happens

Trigger: Calling ScrollToHorizontalOffset(double.NaN), typically from a data binding or computed value that produced NaN (e.g. 0/0 from width calculations, unset double in XAML evaluating to NaN).

Common situations: Binding the offset to a property whose getter computes NaN during layout passes; passing Double.NaN as a 'no value' sentinel; math on ActualWidth before layout completes.

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/1c0d6348c88558b3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/TextBoxBase.cs:279

        /// Vertically scroll to the end of the content.
        /// </summary>
        public void ScrollToEnd()
        {
            if (this.ScrollViewer != null)
            {
                UpdateLayout();
                this.ScrollViewer.ScrollToEnd();
            }
        }

        /// <summary>
        /// Scroll horizontally to the specified offset.
        /// </summary>
        public void ScrollToHorizontalOffset(double offset)
        {
            if (Double.IsNaN(offset))
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (this.ScrollViewer != null)
            {
                UpdateLayout();
                this.ScrollViewer.ScrollToHorizontalOffset(offset);
            }
        }

        /// <summary>
        /// Scroll vertically to the specified offset.
        /// </summary>
        public void ScrollToVerticalOffset(double offset)
        {
            if (Double.IsNaN(offset))
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

View on GitHub (pinned to 81131a70a4)