dotnet/wpf · error · ArgumentOutOfRangeException

SR.PropertyValueCannotBeNaN (MaxTextHeight)

Error message

SR.PropertyValueCannotBeNaN (MaxTextHeight)

What it means

FormattedText.MaxTextHeight setter throws ArgumentOutOfRangeException when the assigned value is double.NaN. After the >0 check, the setter explicitly rejects NaN because an unbounded sentinel in WPF text layout is PositiveInfinity, not NaN.

Solutions

  1. Check double.IsNaN(height) before assigning and replace with double.PositiveInfinity if unconstrained height is intended.
  2. Fix the upstream calculation so it yields a finite positive height instead of NaN.
  3. Use double.PositiveInfinity explicitly for 'no height limit' rather than NaN.

Example fix

// before
formattedText.MaxTextHeight = computedHeight; // may be NaN

// after
formattedText.MaxTextHeight = double.IsNaN(computedHeight) ? double.PositiveInfinity : computedHeight;
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(height)) height = double.PositiveInfinity;
formattedText.MaxTextHeight = height;

Type guard

bool IsFinitePositive(double v) => !double.IsNaN(v) && !double.IsNegativeInfinity(v) && v > 0;

Prevention

When it happens

Trigger: Assigning double.NaN to FormattedText.MaxTextHeight, typically via a databound/computed value where a division by zero or an unset measurement produced NaN.

Common situations: Propagating NaN from layout math (e.g., 0/0 when measuring available space) or from a double property that was never initialized, then handing it to FormattedText for text rendering.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FormattedText.cs:1302

        {
            return (_maxTextWidths == null) ? null : (double [])_maxTextWidths.Clone();
        }

        /// <summary>
        /// Sets the maximum length of a column of text.
        /// The last line of text displayed is the last whole line that will fit within this limit,
        /// or the nth line as specified by MaxLineCount, whichever occurs first.
        /// Use the Trimming property to control how the omission of text is indicated.
        /// </summary>
        public double MaxTextHeight
        {
            set
            {
                if (value <= 0)
                    throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.PropertyMustBeGreaterThanZero, "MaxTextHeight"));

                if (double.IsNaN(value))
                    throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.PropertyValueCannotBeNaN, "MaxTextHeight"));

                _maxTextHeight = value;
                InvalidateMetrics();
            }
            get
            {
                return _maxTextHeight;
            }
        }

        /// <summary>
        /// Defines the maximum number of lines to display.
        /// The last line of text displayed is the lineCount-1'th line,
        /// or the last whole line that will fit within the count set by MaxTextHeight,
        /// whichever occurs first.
        /// Use the Trimming property to control how the omission of text is indicated
        /// </summary>
        public int MaxLineCount

View on GitHub (pinned to 81131a70a4)