AvaloniaUI/Avalonia · error · ExpressionParseException

Too many arguments in RelativeSource syntax sugar

Error message

Too many arguments in RelativeSource syntax sugar

What it means

Thrown by ParseRelativeSource when parsing the '$parent[...]' syntax sugar and ParseArguments returns a count that is 0 or greater than 2. The ancestor syntax supports at most two bracketed arguments: an optional type name and an optional ancestor level (as '$parent[Type;level]', '$parent[Type]', or '$parent[level]'). Zero arguments (empty brackets) or three or more are invalid.

Source

Thrown at src/Avalonia.Base/Data/Core/Parsers/BindingExpressionGrammar.cs:335

        private static State ParseRelativeSource(ref CharacterReader r, List<INode> nodes)
        {
            var mode = r.ParseIdentifier();

            if (mode.SequenceEqual("self".AsSpan()))
            {
                nodes.Add(new SelfNode());
            }
            else if (mode.SequenceEqual("parent".AsSpan()))
            {
                string? ancestorNamespace = null;
                string? ancestorType = null;
                var ancestorLevel = 0;
                if (PeekOpenBracket(ref r))
                {
                    var args = r.ParseArguments('[', ']', ';');
                    if (args.Count > 2 || args.Count == 0)
                    {
                        throw new ExpressionParseException(r.Position, "Too many arguments in RelativeSource syntax sugar");
                    }
                    else if (args.Count == 1)
                    {
                        if (int.TryParse(args[0], out int level))
                        {
                            ancestorType = null;
                            ancestorLevel = level;
                        }
                        else
                        {
                            var reader = new CharacterReader(args[0].AsSpan());
                            (ancestorNamespace, ancestorType) = ParseTypeName(ref reader);
                        }
                    }
                    else
                    {
                        var reader = new CharacterReader(args[0].AsSpan());
                        (ancestorNamespace, ancestorType) = ParseTypeName(ref reader);

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use at most two arguments inside '$parent[...]': either '$parent[Type]', '$parent[3]' (level), or '$parent[Type;3]' (both).
  2. Separate arguments with ';' not ','.
  3. If no arguments are needed, use '$parent' without brackets.

Example fix

<!-- before: too many arguments -->
{Binding $parent[Panel;2;extra]}

<!-- after: correct argument count -->
{Binding $parent[Panel;2]}
Defensive patterns

Strategy: validation

Validate before calling

// Validate $parent bracket argument count (1 or 2, separated by ';')
static void ValidateRelativeSourceArgs(string path)
{
    var match = System.Text.RegularExpressions.Regex.Match(path, @"\$parent\[([^\]]*)\]");
    if (match.Success)
    {
        var args = match.Groups[1].Value.Split(';');
        if (args.Length == 0 || args.Length > 2 || string.IsNullOrEmpty(match.Groups[1].Value))
            throw new ArgumentException(
                "$parent[...] must have 1 or 2 arguments separated by ';'");
    }
}

Try / catch

try
{
    var binding = new Binding(path);
}
catch (ExpressionParseException ex) when (ex.Message.Contains("Too many arguments"))
{
    logger.LogError($"Too many arguments in $parent[...] at column {ex.Column}: use at most Type;level");
}

Prevention

When it happens

Trigger: Writing '$parent[]' (zero arguments) or '$parent[Type;level;extra]' (three arguments separated by ';'). ParseArguments with delimiter ';' splits the bracket content; if the result has more than 2 elements or none at all, this error fires.

Common situations: Attempting to pass additional parameters to the ancestor finder; misunderstanding the RelativeSource syntax and using commas instead of semicolons (commas are the default delimiter, but this overload uses ';'); empty brackets from a template that conditionally inserts ancestor arguments but produces an empty string.

Related errors


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