dotnet/wpf · error · ArgumentOutOfRangeException

SR.Format(SR.PropertyMustBeGreaterThanZero, "MaxTextHeight")

Error message

SR.Format(SR.PropertyMustBeGreaterThanZero, "MaxTextHeight")

What it means

FormattedText.MaxTextHeight setter throws ArgumentOutOfRangeException when the assigned value is <= 0. MaxTextHeight bounds the height of the text layout, so a non-positive value is meaningless and rejected before it invalidates layout metrics.

Solutions

  1. Ensure the value assigned to MaxTextHeight is strictly greater than 0 before setting it.
  2. Clamp or substitute a sensible default (e.g., double.PositiveInfinity for unconstrained height) when the computed height is 0 or negative.
  3. Guard against NaN separately — NaN fails the later NaN check, but negative infinity fails this one first.

Example fix

// before
formattedText.MaxTextHeight = availableHeight; // availableHeight may be 0

// after
formattedText.MaxTextHeight = Math.Max(availableHeight, 1.0);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(height) || height <= 0)
    height = height == 0 || double.IsNaN(height) ? double.PositiveInfinity : 1.0;
formattedText.MaxTextHeight = height;

Type guard

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

Prevention

When it happens

Trigger: Assigning MaxTextHeight a value of 0, a negative number, or double.NegativeInfinity on a System.Windows.Media.FormattedText instance.

Common situations: Computing the max height from a measurement that returned 0 (e.g., an empty element's ActualHeight, an unmeasured control, or a failed size computation) and passing it straight to MaxTextHeight before rendering text.

Related errors


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

Appendix: source

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

        /// </summary>
        /// <returns>The copy of max text width array</returns>
        public double [] GetMaxTextWidths()
        {
            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.

View on GitHub (pinned to 81131a70a4)