AvaloniaUI/Avalonia · error · ExpressionParseException

Indexer may not be empty.

Error message

Indexer may not be empty.

What it means

Thrown by ParseIndexer when ParseArguments('[', ']') returns an empty list — meaning the binding path contains '[]' with no arguments between the brackets. Avalonia's binding mini-language requires at least one indexer argument (e.g. '[0]' or '[key]'); an empty indexer has no meaning and cannot resolve to a property accessor.

Source

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

            }

            nodes.Add(new AttachedPropertyNameNode
            {
                AcceptsNull = acceptsNull,
                Namespace = ns,
                TypeName = owner,
                PropertyName = name.ToString()
            });
            return State.AfterMember;
        }

        private static State ParseIndexer(ref CharacterReader r, List<INode> nodes)
        {
            var args = r.ParseArguments('[', ']');

            if (args.Count == 0)
            {
                throw new ExpressionParseException(r.Position, "Indexer may not be empty.");
            }

            nodes.Add(new IndexerNode { Arguments = args });
            return State.AfterMember;
        }

        private static State ParseTypeCast(ref CharacterReader r, List<INode> nodes)
        {
            bool parseMemberBeforeAddCast = ParseOpenBrace(ref r);

            var (ns, typeName) = ParseTypeName(ref r);

            var result = State.AfterMember;

            if (parseMemberBeforeAddCast)
            {
                if (!ParseCloseBrace(ref r))
                {

View on GitHub (pinned to 11c5427268)

Solutions

  1. Provide at least one argument inside the indexer brackets, e.g. '[0]' or '[key]'.
  2. If building indexer paths dynamically, ensure the index expression is non-empty before constructing the bracket segment.
  3. Replace '[]' with a concrete index value or remove the indexer entirely if no indexing is needed.

Example fix

<!-- before: empty indexer -->
<TextBox Text="{Binding Items[]}" />

<!-- after: concrete index -->
<TextBox Text="{Binding Items[0]}" />
Defensive patterns

Strategy: validation

Validate before calling

// Validate that indexer brackets are not empty
static void ValidateIndexerNotEmpty(string path)
{
    for (int i = 0; i < path.Length - 1; i++)
{
        if (path[i] == '[' && path[i + 1] == ']')
            throw new ArgumentException($"Empty indexer '[]' at position {i} in binding path");
    }
}

Try / catch

try
{
    var binding = new Binding(path);
}
catch (ExpressionParseException ex) when (ex.Message.Contains("Indexer may not be empty"))
{
    logger.LogError($"Empty indexer at column {ex.Column}: provide at least one index argument");
}

Prevention

When it happens

Trigger: Writing 'Foo[]' in a binding path string. ParseArguments returns an empty list only when the open bracket is immediately followed by the close bracket with no argument characters in between (ParseArguments would actually throw 'Expected indexer argument' before returning empty for whitespace-only content, but for the literal '[]' case the while loop body never executes and args.Count stays 0 — actually ParseArguments takes the '[', enters the while loop only if !r.End, and on seeing ']' immediately, r.TakeWhile returns empty and ParseArguments throws 'Expected indexer argument'. So this specific error fires when ParseArguments somehow returns count 0, which occurs when the open bracket is not found at all and ParseArguments returns early).

Common situations: Dynamic index generation that produces an empty string inside the brackets; typo where the indexer value was deleted; misunderstanding the binding mini-language thinking '[]' is valid syntax for a default index.

Related errors


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