{"record":{"id":"8793ea0ecf56bc42","repo":"AvaloniaUI/Avalonia","slug":"bindingexpression-was-already-attached","errorCode":null,"errorMessage":"BindingExpression was already attached.","messagePattern":"BindingExpression was already attached\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs","lineNumber":267,"sourceCode":"    /// <summary>\n    /// When overridden in a derived class, writes the specified value to the binding source if\n    /// possible.\n    /// </summary>\n    /// <param name=\"value\">The value to write.</param>\n    /// <returns>\n    /// True if the value could be written to the binding source; otherwise false.\n    /// </returns>\n    internal virtual bool WriteValueToSource(object? value) => false;\n\n    private void AttachCore(\n        IBindingExpressionSink sink,\n        ImmediateValueFrame? frame,\n        AvaloniaObject target,\n        AvaloniaProperty? targetProperty,\n        BindingPriority priority)\n    {\n        if (_sink is not null)\n            throw new InvalidOperationException(\"BindingExpression was already attached.\");\n        if (TargetProperty is not null && TargetProperty != targetProperty)\n            throw new InvalidOperationException(\"BindingExpression was already attached to a different property.\");\n\n        _sink = sink;\n        _frame = frame;\n        _target = new(target);\n        TargetProperty = targetProperty;\n        TargetType = targetProperty?.PropertyType ?? typeof(object);\n        Priority = priority;\n    }\n\n\n    /// <summary>\n    /// Converts a value using a value converter, logging a warning if necessary.\n    /// </summary>\n    /// <param name=\"converter\">The value converter.</param>\n    /// <param name=\"converterCulture\">The culture to use for the conversion.</param>\n    /// <param name=\"converterParameter\">The converter parameter.</param>","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs#L249-L285","documentation":"AttachCore wires a binding expression to exactly one sink/target. If `_sink` is already set, the expression is already attached and attaching again is an error, because a single expression instance cannot back two targets. This preserves one-to-one attachment semantics.","triggerScenarios":"Calling Start/Attach twice on the same binding expression instance, or sharing one expression object across two AvaloniaObject targets.","commonSituations":"Reusing a single IBinding/Expression object to bind two different controls. Caching an expression instance and binding it again after it was already attached.","solutions":["Create a new expression instance for each target via `IBinding.CreateInstance(...)` (each IBinding represents a template that produces fresh instances).","Do not cache or reuse binding expression instances across controls; create per-target.","If you must bind many controls, loop and call CreateInstance for each."],"exampleFix":"// before (reusing one expr):\nvar expr = myBinding.CreateInstance(a, prop, anchor);\nexpr.Start(...);\nexpr.Start(...); // throws on second attach\n\n// after:\nforeach (var c in controls)\n    c.Bind(prop, myBinding); // CreateInstance per target","handlingStrategy":"validation","validationCode":"// Each target gets its own expression instance.\nvar expr = binding.CreateInstance(target, prop, anchor); // attach once","typeGuard":"static bool IsUnattached(UntypedBindingExpressionBase e) => /* _sink */ false; // conceptual","tryCatchPattern":null,"preventionTips":["Call CreateInstance per target; never reuse an attached expression.","Do not cache expression instances across controls.","Treat IBinding as a factory, the expression as a single-use product."],"tags":["bindings","bindingexpression","attach","invalidoperation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}