AvaloniaUI/Avalonia · error · ExpressionParseException

Element name expected after '#'.

Error message

Element name expected after '#'.

What it means

Thrown by ParseElementName when the '#' character (element-name reference operator) is consumed but ParseIdentifier returns an empty span. In Avalonia's binding mini-language, '#name' resolves to a named control in the visual tree; the '#' must be followed by a valid identifier that matches an x:Name or Name attribute on a control.

Source

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

            }

            nodes.Add(new TypeCastNode { Namespace = ns, TypeName = typeName });

            if (r.End || !r.TakeIf(')'))
            {
                throw new ExpressionParseException(r.Position, "Expected ')'.");
            }

            return result;
        }

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

            if (name.IsEmpty)
            {
                throw new ExpressionParseException(r.Position, "Element name expected after '#'.");
            }

            nodes.Add(new NameNode { Name = name.ToString() });
            return State.AfterMember;
        }

        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;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Add a valid control name after '#', e.g. '#myButton'.
  2. Ensure the element name starts with a letter or underscore and contains only identifier-valid characters.
  3. If building the name dynamically, check for null/empty before constructing the '#name' path.

Example fix

<!-- before: '#' with no name -->
<TextBox Text="{Binding #}" />

<!-- after: valid element name -->
<TextBox Text="{Binding #myButton}" />
Defensive patterns

Strategy: validation

Validate before calling

// Validate that '#' in a binding path is followed by a valid identifier
static void ValidateElementName(string path)
{
    for (int i = 0; i < path.Length; i++)
    {
        if (path[i] == '#')
        {
            if (i + 1 >= path.Length)
                throw new ArgumentException("'#' at end of path with no element name");
            char next = path[i + 1];
            if (!char.IsLetter(next) && next != '_')
                throw new ArgumentException($"Invalid element name start '{next}' after '#' at position {i}");
        }
    }
}

Try / catch

try
{
    var binding = new Binding(path);
}
catch (ExpressionParseException ex) when (ex.Message.Contains("Element name expected"))
{
    logger.LogError($"Missing element name after '#' at column {ex.Column}");
}

Prevention

When it happens

Trigger: Writing '#' at end of the binding string with no name following, or '#' followed by non-identifier characters such as digits or symbols at the start. ParseIdentifier only accepts characters valid in C# identifiers (letters, underscore, then letters/digits/underscores), so '#123' or '#@foo' would produce an empty identifier.

Common situations: Typo or incomplete binding path where '#myButton' was truncated to '#'; dynamic binding construction that concatenates an element name variable that happens to be empty or null; using '#' to reference a control but the name contains leading digits which are not valid identifier-start characters.

Related errors


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