{"record":{"id":"2fe1977cbaf86d96","repo":"dotnet/maui","slug":"cannot-change-a-binding-while-it-s-applied","errorCode":null,"errorMessage":"Cannot change a binding while it's applied","messagePattern":"Cannot change a binding while it's applied","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/BindingBase.cs","lineNumber":160,"sourceCode":"\t\t/// </remarks>\n\t\tpublic static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)\n\t\t{\n\t\t\tif (collection == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(collection));\n\t\t\tif (callback == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(callback));\n\n\t\t\tSynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));\n\t\t}\n\n\t\t/// <summary>Throws <see cref=\"InvalidOperationException\" /> if the binding has already been applied.</summary>\n\t\t/// <remarks>Used by property setters to prevent mutation after the binding has been attached to a target.</remarks>\n\t\t/// <exception cref=\"InvalidOperationException\">The binding has already been applied.</exception>\n\t\t[MethodImpl(MethodImplOptions.AggressiveInlining)]\n\t\tprotected void ThrowIfApplied()\n\t\t{\n\t\t\tif (IsApplied)\n\t\t\t\tthrow new InvalidOperationException(\"Cannot change a binding while it's applied\");\n\t\t}\n\n\t\tinternal virtual void Apply(bool fromTarget)\n\t\t\t\t=> IsApplied = true;\n\n\t\tinternal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged, SetterSpecificity specificity)\n\t\t\t=> IsApplied = true;\n\n\t\tinternal abstract BindingBase Clone();\n\n\t\tinternal virtual object GetSourceValue(object value, Type targetPropertyType)\n\t\t{\n\t\t\tif (value == null && TargetNullValue != null)\n\t\t\t\treturn TargetNullValue;\n\n\t\t\tif (StringFormat != null && TryFormat(StringFormat, value, out var formatted))\n\t\t\t\treturn formatted;\n","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/BindingBase.cs#L142-L178","documentation":"Thrown by ThrowIfApplied() (called from many property setters) when IsApplied is true, meaning the BindingBase has already been attached to a target BindableObject. MAUI bindings are not designed to be mutated after application; setters call ThrowIfApplied to enforce this invariant.","triggerScenarios":"Setting Mode, Path, Converter, Source, StringFormat, or any other property on a Binding after it has been applied to a BindableObject via SetBinding. Reusing the same Binding instance and mutating it after it is live. Applying a binding then trying to reconfigure it in a handler.","commonSituations":"Caching a Binding instance and reconfiguring it on view reuse. Setting binding properties inside an event handler that fires after the binding is already attached. Misunderstanding that SetBinding finalizes the binding.","solutions":["Create a fresh Binding instance whenever configuration must change; never mutate an applied binding.","Set all desired properties before calling SetBinding on the target.","Call RemoveBinding / re-create the binding rather than editing properties in place."],"exampleFix":"// before — mutating after apply\nelement.SetBinding(Label.TextProperty, binding);\nbinding.StringFormat = \"{0:C}\"; // throws\n\n// after\nvar binding = new Binding(\"Price\") { StringFormat = \"{0:C}\" };\nelement.SetBinding(Label.TextProperty, binding);","handlingStrategy":"validation","validationCode":"// Configure fully BEFORE applying; never mutate after SetBinding\nvar binding = new Binding(\"Name\")\n{\n    Mode = BindingMode.OneWay,\n    StringFormat = \"{0}\"\n};\nelement.SetBinding(Label.TextProperty, binding);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat Binding as immutable after SetBinding.","Build a new Binding instance when configuration must change.","Cache binding configuration data, not live binding instances."],"tags":["maui","binding","state","immutability"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}