{"record":{"id":"10b390441259641e","repo":"AvaloniaUI/Avalonia","slug":"element-name-expected-after","errorCode":null,"errorMessage":"Element name expected after '#'.","messagePattern":"Element name expected after '#'\\.","errorType":"validation","errorClass":"ExpressionParseException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/Parsers/BindingExpressionGrammar.cs","lineNumber":310,"sourceCode":"            }\n\n            nodes.Add(new TypeCastNode { Namespace = ns, TypeName = typeName });\n\n            if (r.End || !r.TakeIf(')'))\n            {\n                throw new ExpressionParseException(r.Position, \"Expected ')'.\");\n            }\n\n            return result;\n        }\n\n        private static State ParseElementName(ref CharacterReader r, List<INode> nodes)\n        {\n            var name = r.ParseIdentifier();\n\n            if (name.IsEmpty)\n            {\n                throw new ExpressionParseException(r.Position, \"Element name expected after '#'.\");\n            }\n\n            nodes.Add(new NameNode { Name = name.ToString() });\n            return State.AfterMember;\n        }\n\n        private static State ParseRelativeSource(ref CharacterReader r, List<INode> nodes)\n        {\n            var mode = r.ParseIdentifier();\n\n            if (mode.SequenceEqual(\"self\".AsSpan()))\n            {\n                nodes.Add(new SelfNode());\n            }\n            else if (mode.SequenceEqual(\"parent\".AsSpan()))\n            {\n                string? ancestorNamespace = null;\n                string? ancestorType = null;","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/Parsers/BindingExpressionGrammar.cs#L292-L328","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a valid control name after '#', e.g. '#myButton'.","Ensure the element name starts with a letter or underscore and contains only identifier-valid characters.","If building the name dynamically, check for null/empty before constructing the '#name' path."],"exampleFix":"<!-- before: '#' with no name -->\n<TextBox Text=\"{Binding #}\" />\n\n<!-- after: valid element name -->\n<TextBox Text=\"{Binding #myButton}\" />","handlingStrategy":"validation","validationCode":"// Validate that '#' in a binding path is followed by a valid identifier\nstatic void ValidateElementName(string path)\n{\n    for (int i = 0; i < path.Length; i++)\n    {\n        if (path[i] == '#')\n        {\n            if (i + 1 >= path.Length)\n                throw new ArgumentException(\"'#' at end of path with no element name\");\n            char next = path[i + 1];\n            if (!char.IsLetter(next) && next != '_')\n                throw new ArgumentException($\"Invalid element name start '{next}' after '#' at position {i}\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var binding = new Binding(path);\n}\ncatch (ExpressionParseException ex) when (ex.Message.Contains(\"Element name expected\"))\n{\n    logger.LogError($\"Missing element name after '#' at column {ex.Column}\");\n}","preventionTips":["Verify that the control referenced by '#name' exists with a matching x:Name or Name attribute.","Ensure element names start with a letter or underscore.","When building element-name paths dynamically, check for null or empty name variables."],"tags":["avalonia","binding","xaml","binding-expression","element-name","parser","syntax-error"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}