AvaloniaUI/Avalonia · error · FormatException

If one coordinate is relative, all must be.

Error message

If one coordinate is relative, all must be.

What it means

Thrown by RelativeRect.Parse when some but not all four components (x, y, width, height) carry a '%' suffix. A RelativeRect must be uniformly absolute or uniformly relative; a partially-relative rect like '10,10,50%,20' is rejected as a FormatException.

Source

Thrown at src/Avalonia.Base/RelativeRect.cs:206

                var xRelative = x.EndsWith(PercentChar, StringComparison.Ordinal);
                var yRelative = y.EndsWith(PercentChar, StringComparison.Ordinal);
                var widthRelative = width.EndsWith(PercentChar, StringComparison.Ordinal);
                var heightRelative = height.EndsWith(PercentChar, StringComparison.Ordinal);

                if (xRelative && yRelative && widthRelative && heightRelative)
                {
                    x = x.TrimEnd(PercentChar);
                    y = y.TrimEnd(PercentChar);
                    width = width.TrimEnd(PercentChar);
                    height = height.TrimEnd(PercentChar);

                    unit = RelativeUnit.Relative;
                    scale = 0.01;
                }
                else if (xRelative || yRelative || widthRelative || heightRelative)
                {
                    throw new FormatException("If one coordinate is relative, all must be.");
                }

                return new RelativeRect(
                    SpanHelpers.ParseDouble(x, CultureInfo.InvariantCulture) * scale,
                    SpanHelpers.ParseDouble(y, CultureInfo.InvariantCulture) * scale,
                    SpanHelpers.ParseDouble(width, CultureInfo.InvariantCulture) * scale,
                    SpanHelpers.ParseDouble(height, CultureInfo.InvariantCulture) * scale,
                    unit);
            }
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Make all four components the same unit: "10,10,50,50" or "10%,10%,50%,50%".
  2. For partially-relative rects use an explicit layout primitive (RelativeScalar columns/rows or a computed Rect) instead of a single mixed string.
  3. Pre-validate the string splits into 4 tokens with identical suffix handling.

Example fix

<!-- before -->
<VisualBrush DestinationRect="10, 10, 50%, 50" />
<!-- after -->
<VisualBrush DestinationRect="10%,10%,50%,50%" />
Defensive patterns

Strategy: validation

Validate before calling

static bool IsUniformRect(string s)
{
    var parts = s.Split(',', StringSplitOptions.TrimEntries);
    if (parts.Length != 4) return false;
    bool rel = parts[0].EndsWith('%');
    return parts.All(p => p.EndsWith('%') == rel);
}
if (!IsUniformRect(s)) throw new FormatException("All four rect components must share the same unit (%).");
RelativeRect.Parse(s);

Try / catch

try { RelativeRect.Parse(s); }
catch (FormatException) { /* surface markup error */ throw; }

Prevention

When it happens

Trigger: Parsing a string like "10, 10, 50%, 50" via RelativeRect.Parse (from XAML or converter) where one or more — but not all — tokens end in '%'.

Common situations: XAML rect markup authored with mixed units intentionally but unsupported by the format; a converter appends '%' to width/height but not x/y; copy-paste between RelativePoint and RelativeRect leaving inconsistent units.

Related errors


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