{"record":{"id":"d8394da5ef6fb7e2","repo":"dotnet/maui","slug":"binding-instances-cannot-be-reused","errorCode":null,"errorMessage":"Binding instances cannot be reused","messagePattern":"Binding instances cannot be reused","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/BindingExpression.cs","lineNumber":90,"sourceCode":"  \t\t\t\t// In a DataTemplate, x:DataType describes the item context - not the type of the referenced element.\n   \t\t\t\t// RelativeBindingSource is excluded: a mismatch against its source type must still be reported.\n\t\t\t\tbool skipTypeMismatchCheck = (source is not null && source is not RelativeBindingSource)\n\t\t\t\t\t|| !RuntimeFeature.IsXamlCBindingWithSourceCompilationEnabled;\n\t\t\t\tif (!skipTypeMismatchCheck)\n\t\t\t\t{\n\t\t\t\t\tif (sourceObject != null && !dataType.IsAssignableFrom(sourceObject.GetType()))\n\t\t\t\t\t{\n\t\t\t\t\t\tBindingDiagnostics.SendBindingFailure(Binding, \"Binding\", $\"Mismatch between the specified x:DataType ({dataType}) and the current binding context ({sourceObject.GetType()}).\");\n\t\t\t\t\t\tsourceObject = null;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t_targetProperty = property;\n\t\t\t_specificity = specificity;\n\n\t\t\tif (_weakTarget != null && _weakTarget.TryGetTarget(out BindableObject prevTarget) && !ReferenceEquals(prevTarget, target))\n\t\t\t\tthrow new InvalidOperationException(\"Binding instances cannot be reused\");\n\n\t\t\tif (_weakSource != null && _weakSource.TryGetTarget(out var previousSource) && !ReferenceEquals(previousSource, sourceObject))\n\t\t\t\tthrow new InvalidOperationException(\"Binding instances cannot be reused\");\n\n\t\t\t_weakSource = new WeakReference<object>(sourceObject);\n\t\t\t_weakTarget = new WeakReference<BindableObject>(target);\n\n\t\t\tApplyCore(sourceObject, target, property, false, specificity);\n\t\t}\n\n\t\tinternal void Unapply()\n\t\t{\n\t\t\tif (_weakSource != null && _weakSource.TryGetTarget(out var sourceObject))\n\t\t\t{\n\t\t\t\tfor (var i = 0; i < _parts.Count - 1; i++)\n\t\t\t\t{\n\t\t\t\t\tBindingExpressionPart part = _parts[i];\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/BindingExpression.cs#L72-L108","documentation":"Thrown by BindingExpression.Apply when the weak reference to a previously-bound target resolves to a different BindableObject than the one now being applied. MAUI Binding instances are single-use for a given target; reusing one against a different target throws to prevent corrupted state.","triggerScenarios":"Calling SetBinding with the same Binding instance on two different BindableObject targets. Sharing a cached Binding across multiple controls. Re-applying a binding recovered from a pooled view to a new view instance.","commonSituations":"Storing a Binding in a static/field and applying it to multiple dynamically created controls. View recycling logic that reuses binding objects along with views. Copying bindings from a template element to siblings.","solutions":["Create a separate Binding instance per target; never share a single instance across controls.","Use Clone() if you need an equivalent binding for a new target.","In recycling scenarios, build bindings in the template/data-selectors rather than caching instances."],"exampleFix":"// before — one binding, two targets\nvar b = new Binding(\"Name\");\nlabel1.SetBinding(Label.TextProperty, b);\nlabel2.SetBinding(Label.TextProperty, b); // throws\n\n// after\nlabel1.SetBinding(Label.TextProperty, new Binding(\"Name\"));\nlabel2.SetBinding(Label.TextProperty, new Binding(\"Name\"));","handlingStrategy":"validation","validationCode":"// Never reuse; create per target\nstatic Binding Make(string path) => new Binding(path);\nlabel1.SetBinding(Label.TextProperty, Make(\"Name\"));\nlabel2.SetBinding(Label.TextProperty, Make(\"Name\"));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Allocate a fresh Binding for each target BindableObject.","Use Clone() to copy a binding to a new target.","Never store a single Binding in a shared field/static for reuse across controls."],"tags":["maui","binding","reuse","single-use"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}