AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException

'MaxTextHeight' property value must be greater than zero.

Error message

'MaxTextHeight' property value must be greater than zero.

What it means

FormattedText.MaxTextHeight setter rejects non-positive values because layout divides/clamps against the column height and needs a positive bound. Unlike MaxTextWidth (where 0 means unbounded), MaxTextHeight treats 0 as invalid.

Source

Thrown at src/Avalonia.Base/Media/FormattedText.cs:1146

        /// <returns>The copy of max text width array</returns>
        public double[] GetMaxTextWidths()
        {
            return _maxTextWidths != null ? (double[])_maxTextWidths.Clone() : Array.Empty<double>();
        }

        /// <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), $"'{nameof(MaxTextHeight)}' property value must be greater than zero.");
                }

                if (double.IsNaN(value))
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"'{nameof(MaxTextHeight)}' property value cannot be NaN.");
                }

                _maxTextHeight = value;

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

        /// <summary>

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use a sentinel meaning 'no limit' (e.g. double.PositiveInfinity) elsewhere and only assign MaxTextHeight when a positive bound is genuinely wanted.
  2. Defer the assignment until the container has a measured positive height.
  3. Coerce to a minimum positive value (e.g. line height) before setting.
  4. Initialize the backing field to a sensible positive default.

Example fix

// before
ft.MaxTextHeight = container.ActualHeight; // 0 during first layout pass

// after
double h = container.ActualHeight;
ft.MaxTextHeight = h > 0 ? h : double.PositiveInfinity; // or omit the limit
Defensive patterns

Strategy: validation

Validate before calling

double h = double.IsFinite(value) && value > 0 ? value : double.PositiveInfinity;
if (double.IsFinite(h)) ft.MaxTextHeight = h;

Prevention

When it happens

Trigger: Binding MaxTextHeight to an unmeasured container; passing 0 from an uninitialized field; computing from a padding subtraction that yields 0 or negative on small containers.

Common situations: A 'show more text' UI where the collapsed height is set to 0 incorrectly; binding to ActualHeight during initial layout (still 0); using a default double value of 0.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/a080604a43347629. Report an issue: GitHub.