{"record":{"id":"cb94dc38c717ce10","repo":"AvaloniaUI/Avalonia","slug":"unable-to-parse-expression-expected-an-instan","errorCode":null,"errorMessage":"Unable to parse '{expression}': expected an instance of '{_head}' but got '{visited}'.","messagePattern":"Unable to parse '(.+?)': expected an instance of '(.+?)' but got '(.+?)'\\.","errorType":"exception","errorClass":"ExpressionParseException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/Parsers/BindingExpressionVisitor.cs","lineNumber":274,"sourceCode":"    }\n\n    protected override Expression VisitTry(TryExpression node)\n    {\n        throw new ExpressionParseException(0, $\"Invalid expression type in binding expression: {node.NodeType}.\");\n    }\n\n    protected override Expression VisitTypeBinary(TypeBinaryExpression node)\n    {\n        throw new ExpressionParseException(0, $\"Invalid expression type in binding expression: {node.NodeType}.\");\n    }\n\n    private Expression Add(Expression? instance, Expression expression, Action<CompiledBindingPathBuilder> build)\n    {\n        var visited = Visit(instance);\n\n        if (visited != _head)\n        {\n            throw new ExpressionParseException(\n                0,\n                $\"Unable to parse '{expression}': expected an instance of '{_head}' but got '{visited}'.\");\n        }\n\n        build(_builder);\n        return _head = expression;\n    }\n\n    private Expression AddPropertyNode(MemberExpression node)\n    {\n        // Check if it's an AvaloniaProperty accessed via CLR wrapper\n        if (typeof(AvaloniaObject).IsAssignableFrom(node.Expression?.Type) &&\n            AvaloniaPropertyRegistry.Instance.FindRegistered(node.Expression.Type, node.Member.Name) is { } avaloniaProperty)\n        {\n            return Add(\n                node.Expression,\n                node,\n                x => x.Property(avaloniaProperty, CreateAvaloniaPropertyAccessor));","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/Parsers/BindingExpressionVisitor.cs#L256-L292","documentation":"BindingExpressionVisitor maintains a single `_head` representing the current end of the path. The private Add method re-visits the instance of each new node and requires it to equal `_head`; if the visited instance is not the current head (broken chain, method on a different object, static that detaches from the parameter), it throws ExpressionParseException. This is the generic 'the expression is not a continuous path rooted at the lambda parameter' error.","triggerScenarios":"CompiledBinding.For(x => Helper.Format(x.Name)) where the method operates on a different object than the path head; mixing unrelated sub-expressions; a StreamBinding or delegate call applied to something other than the current path result.","commonSituations":"Calling static/instance helpers on objects other than the current path result; building expression trees where a node's instance does not chain from the previous node; incorrect StreamBinding placement.","solutions":["Make the binding a single continuous chain rooted at the lambda parameter (x => x.A.B.C).","Move helper calls into a view-model property or an IValueConverter.","Apply StreamBinding to the current path tail: x => x.Loader.StreamBinding()."],"exampleFix":"// before\nCompiledBinding.For(x => Helper.Format(x.Name));\n// after - expose formatted name on the VM\nCompiledBinding.For(x => x.FormattedName);","handlingStrategy":"validation","validationCode":"// Verify the lambda body chains continuously from its single parameter\n// (every node's instance resolves to the previous node's result).\n// Quick check: the body must reference exactly the lambda's parameter as root.\nvar param = expr.Parameters[0];\nbool rooted = new RootChecker(param).IsRooted(expr.Body);","typeGuard":null,"tryCatchPattern":"try { var path = BindingExpressionVisitor<TIn>.BuildPath<TOut>(expr); }\ncatch (ExpressionParseException ex) when (ex.Message.Contains(\"expected an instance of\"))\n{\n    // restructure the lambda into a single continuous chain\n}","preventionTips":["Write binding lambdas as one continuous chain from the parameter (x => x.A.B.C).","Do not call static or instance helpers on objects other than the current path result.","Apply StreamBinding to the tail of the chain only."],"tags":["compiled-binding","expression-tree","binding-path","binding"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}