{"record":{"id":"e26a545d28fa6002","repo":"AvaloniaUI/Avalonia","slug":"element-init-expressions-are-not-valid-in-a-bindin","errorCode":null,"errorMessage":"Element init expressions are not valid in a binding expression.","messagePattern":"Element init expressions are not valid in a binding expression\\.","errorType":"validation","errorClass":"ExpressionParseException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/Parsers/BindingExpressionVisitor.cs","lineNumber":220,"sourceCode":"\n    protected override CatchBlock VisitCatchBlock(CatchBlock node)\n    {\n        throw new ExpressionParseException(0, \"Catch blocks are not allowed in binding expressions.\");\n    }\n\n    protected override Expression VisitConditional(ConditionalExpression node)\n    {\n        throw new ExpressionParseException(0, $\"Invalid expression type in binding expression: {node.NodeType}.\");\n    }\n\n    protected override Expression VisitDynamic(DynamicExpression node)\n    {\n        throw new ExpressionParseException(0, \"Dynamic expressions are not allowed in binding expressions.\");\n    }\n\n    protected override ElementInit VisitElementInit(ElementInit node)\n    {\n        throw new ExpressionParseException(0, \"Element init expressions are not valid in a binding expression.\");\n    }\n\n    protected override Expression VisitGoto(GotoExpression node)\n    {\n        throw new ExpressionParseException(0, \"Goto expressions are not supported in binding expressions.\");\n    }\n\n    protected override Expression VisitInvocation(InvocationExpression node)\n    {\n        throw new ExpressionParseException(0, $\"Invalid expression type in binding expression: {node.NodeType}.\");\n    }\n\n    protected override Expression VisitLabel(LabelExpression node)\n    {\n        throw new ExpressionParseException(0, $\"Invalid expression type in binding expression: {node.NodeType}.\");\n    }\n\n    protected override Expression VisitListInit(ListInitExpression node)","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/Parsers/BindingExpressionVisitor.cs#L202-L238","documentation":"Thrown unconditionally by BindingExpressionVisitor.VisitElementInit when an ElementInit node appears in the compiled binding expression tree. ElementInit nodes are produced by collection initializer expressions (e.g., 'new List<int> { 1, 2, 3 }'). They have no meaning in a binding property path and are always rejected.","triggerScenarios":"Including a collection-initializer expression inside a compiled binding lambda, such as x => new ObservableCollection<int> { 1, 2, 3 }. This is extremely unusual in practice but could arise from copy-pasting initialization code into a binding expression.","commonSituations":"Accidentally including object or collection initialization syntax in a binding lambda; expression-tree manipulation that injects initializer nodes; generated code from a template engine that includes initializer syntax in what should be a path expression.","solutions":["Remove any collection or object initializer syntax from the binding lambda; the lambda should be a simple member-access chain.","If you need to initialize a collection, do so in the view model and bind to the resulting property.","Ensure the binding lambda is a pure property-path expression with no 'new', initializer, or assignment constructs."],"exampleFix":"// before: collection initializer in binding\nCompiledBinding(x => new List<string> { x.Name })\n\n// after: bind to a pre-populated property\nCompiledBinding(x => x.NameList)","handlingStrategy":"validation","validationCode":"// Detect collection initializer expressions in a binding lambda\nstatic bool HasElementInit<TIn, TOut>(Expression<Func<TIn, TOut>> expr)\n{\n    bool found = false;\n    expr.Body.Visit(b =>\n    {\n        if (b is ListInitExpression or NewArrayExpression) found = true;\n    });\n    return !found;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var path = BindingExpressionVisitor<TViewModel>.BuildPath<TProp>(expr);\n}\ncatch (ExpressionParseException ex) when (ex.Message.Contains(\"Element init\"))\n{\n    logger.LogError(\"Collection or element initializers are not valid in compiled binding expressions.\");\n}","preventionTips":["Never include 'new' or initializer syntax in a compiled binding lambda.","Keep binding lambdas as pure property-access chains.","Initialize collections in the view model, then bind to the property."],"tags":["avalonia","binding","compiled-binding","expression-tree","element-init","visitor","unsupported-operation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}