{"record":{"id":"43bc044f2cc7c01c","repo":"AvaloniaUI/Avalonia","slug":"bindingexpression-was-already-attached-to-a-differ","errorCode":null,"errorMessage":"BindingExpression was already attached to a different property.","messagePattern":"BindingExpression was already attached to a different property\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs","lineNumber":269,"sourceCode":"    /// 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>\n    /// <param name=\"value\">The value to convert.</param>\n    /// <param name=\"targetType\">The target type to convert to.</param>","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs#L251-L287","documentation":"AttachCore rejects attaching an already-attached expression to a different AvaloniaProperty than the one it was first bound to. A binding expression is tied to one target property; switching properties indicates a logic error or accidental reuse.","triggerScenarios":"Attaching the same binding expression instance to target property A, then later to target property B (where TargetProperty was already recorded as A).","commonSituations":"Sharing one expression object across two different properties, or rebinding a detached-looking (but not actually detached) expression to a new property.","solutions":["Create a new expression instance per target property via CreateInstance.","Ensure each control+property pair gets its own expression; never reuse one instance across properties.","Audit caching layers that hand out the same expression to multiple property bindings."],"exampleFix":"// before:\nexpr.Attach(..., PropertyA, ...);\nexpr.Attach(..., PropertyB, ...); // throws\n\n// after:\nvar exprA = binding.CreateInstance(t, PropertyA, anchor);\nvar exprB = binding.CreateInstance(t, PropertyB, anchor);","handlingStrategy":"validation","validationCode":"// One expression per (target, property) pair.\nvar exprA = binding.CreateInstance(t, PropertyA, anchor);\nvar exprB = binding.CreateInstance(t, PropertyB, anchor);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never reuse an expression across different properties.","Audit shared/cached expressions for cross-property reuse.","Create fresh instances for each property binding."],"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"}