AvaloniaUI/Avalonia · error · ArgumentException
Could not parse text decoration.
Error message
Could not parse text decoration.
What it means
Thrown by GetTextDecorationLocation when SpanHelpers.TryParseEnum<TextDecorationLocation> fails for a token. The token must match a TextDecorationLocation enum name (case-insensitive, e.g. Baseline, OverLine, Strikethrough, Underline). Any unrecognized word, abbreviation, or stray character fails the parse.
Source
Thrown at src/Avalonia.Base/Media/TextDecorationCollection.cs:69
textDecorations.Add(new TextDecoration { Location = textDecorationLocation });
}
return textDecorations;
}
/// <summary>
/// Parses a <see cref="TextDecorationLocation"/> string.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>The <see cref="TextDecorationLocation"/>.</returns>
private static TextDecorationLocation GetTextDecorationLocation(ReadOnlySpan<char> s)
{
if (SpanHelpers.TryParseEnum<TextDecorationLocation>(s,true, out var location))
{
return location;
}
throw new ArgumentException("Could not parse text decoration.", nameof(s));
}
}
}
View on GitHub (pinned to 11c5427268)
Solutions
- Use the exact TextDecorationLocation enum names: Baseline, OverLine, Strikethrough, Underline (case-insensitive).
- Map CSS names before parsing: 'line-through' -> 'Strikethrough', 'overline' -> 'OverLine'.
- Trim whitespace and validate each token against Enum.GetNames<TextDecorationLocation>() before composing the string.
Example fix
// before
var d = TextDecorationCollection.Parse("line-through,underline");
// after
var d = TextDecorationCollection.Parse("Strikethrough,Underline"); Defensive patterns
Strategy: validation
Validate before calling
static readonly HashSet<string> Valid =
new(Enum.GetNames<TextDecorationLocation>(), StringComparer.OrdinalIgnoreCase);
static bool IsValidDecoration(string tok) => Valid.Contains(tok.Trim()); Type guard
static bool IsValidDecorationName(string s) =>
Enum.GetNames<TextDecorationLocation>().Any(n =>
n.Equals(s, StringComparison.OrdinalIgnoreCase)); Try / catch
try { var d = TextDecorationCollection.Parse(s); }
catch (ArgumentException ex) when (ex.Message.Contains("Could not parse"))
{ /* map CSS names or report invalid token */ } Prevention
- Use exact TextDecorationLocation enum names (Baseline, OverLine, Strikethrough, Underline).
- Map CSS values ('line-through' -> 'Strikethrough') before parsing.
- Validate each token against the enum names when data is user-supplied.
When it happens
Trigger: Strings like 'underline2', 'italic' (not a decoration), 'line-through' (the CSS name, not the enum name), or a typo like 'Underlin'.
Common situations: Mapping CSS text-decoration values directly (CSS uses 'line-through' where Avalonia uses 'Strikethrough'); user-typed decoration names; abbreviations or localized names.
Related errors
- Text decoration already specified.
- Invalid value
- Invalid fill rule.
- Invalid fill rule
- Invalid bool rule.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/201f549db6f50743.
Report an issue: GitHub.