AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException
Parameter must be greater than or equal to zero.
Error message
Parameter must be greater than or equal to zero.
What it means
FormattedText.LineHeight setter rejects negative values because a negative line height makes line-breaking math produce overlapping or negative rectangles and breaks cursor/hit-testing. Zero is allowed (meaning the default line height).
Source
Thrown at src/Avalonia.Base/Media/FormattedText.cs:1061
InvalidateMetrics();
}
get
{
return _defaultParaProps.TextAlignment;
}
}
/// <summary>
/// Gets or sets the height of, or the spacing between, each line where
/// zero represents the default line height.
/// </summary>
public double LineHeight
{
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), "Parameter must be greater than or equal to zero.");
}
_defaultParaProps.SetLineHeight(value);
InvalidateMetrics();
}
get
{
return _defaultParaProps.LineHeight;
}
}
/// <summary>
/// The MaxTextWidth property defines the alignment edges for the FormattedText.
/// For example, left aligned text is wrapped such that the leftmost glyph alignment point
/// on each line falls exactly on the left edge of the rectangle.
/// Note that for many fonts, especially in italic style, some glyph strokes may extend beyond the edges of the alignment rectangle.
/// For this reason, it is recommended that clients draw text with at least 1/6 em (i.e of the font size) unused margin space either side.View on GitHub (pinned to 11c5427268)
Solutions
- Clamp the value to [0, +Inf) before setting: value = Math.Max(0, value).
- Use 0 to mean 'default' rather than a negative sentinel.
- Constrain the input source (slider Minimum=0, numeric input validator).
- Add an IValueConverter that coerces negative bound values to 0.
Example fix
// before ft.LineHeight = requested; // requested may be -1 sentinel // after ft.LineHeight = requested < 0 ? 0 : requested;
Defensive patterns
Strategy: validation
Validate before calling
double lh = double.IsFinite(value) ? Math.Max(0, value) : 0; ft.LineHeight = lh;
Prevention
- Use 0 to mean 'default line height' rather than negative sentinels.
- Constrain line-spacing inputs to Minimum=0.
- Coerce bound values in an IValueConverter.
When it happens
Trigger: Binding LineHeight to a Slider with Minimum < 0; reading LineHeight from a config file where the sign was mis-entered; computing LineHeight as a multiplier that can go negative (e.g. baseHeight * -1).
Common situations: User-customizable line spacing UI without a non-negative constraint; defaulting a nullable double to -1 as a sentinel and forgetting to map it to null/0 before setting LineHeight.
Related errors
- 'MaxTextHeight' property value must be greater than zero.
- The parameter value must be greater than zero.
- The parameter value cannot be greater than '{MaxFontEmSize}'
- The parameter value must be a number.
- 'MaxTextHeight' property value cannot be NaN.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/53725acd92267b4a.
Report an issue: GitHub.