{"record":{"id":"d0db5a595347d5a9","repo":"dotnet/wpf","slug":"sr-changesealedbinding","errorCode":null,"errorMessage":"SR.ChangeSealedBinding","messagePattern":"SR\\.ChangeSealedBinding","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingBase.cs","lineNumber":422,"sourceCode":"            _isSealed = true;\n            return CreateBindingExpressionOverride(targetObject, targetProperty, null);\n        }\n\n        /// <summary>\n        /// Create an appropriate expression for this Binding, to be attached\n        /// to the given DependencyProperty on the given DependencyObject.\n        /// </summary>\n        internal BindingExpressionBase CreateBindingExpression(DependencyObject targetObject, DependencyProperty targetProperty, BindingExpressionBase owner)\n        {\n            _isSealed = true;\n            return CreateBindingExpressionOverride(targetObject, targetProperty, owner);\n        }\n\n        // Throw if the binding is sealed.\n        internal void CheckSealed()\n        {\n            if (_isSealed)\n                throw new InvalidOperationException(SR.ChangeSealedBinding);\n        }\n\n        // Return one of the special ValidationRules\n        internal ValidationRule GetValidationRule(Type type)\n        {\n            if (TestFlag(BindingFlags.ValidatesOnExceptions) && type == typeof(System.Windows.Controls.ExceptionValidationRule))\n                return System.Windows.Controls.ExceptionValidationRule.Instance;\n\n            if (TestFlag(BindingFlags.ValidatesOnDataErrors) && type == typeof(System.Windows.Controls.DataErrorValidationRule))\n                return System.Windows.Controls.DataErrorValidationRule.Instance;\n\n            if (TestFlag(BindingFlags.ValidatesOnNotifyDataErrors) && type == typeof(System.Windows.Controls.NotifyDataErrorValidationRule))\n                return System.Windows.Controls.NotifyDataErrorValidationRule.Instance;\n\n            return LookupValidationRule(type);\n        }\n\n        internal virtual ValidationRule LookupValidationRule(Type type)","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingBase.cs#L404-L440","documentation":"CheckSealed throws when a property of a Binding is modified after the binding has been sealed. Bindings become sealed (_isSealed) once attached to a target so that runtime mutation cannot corrupt an active binding. Changing FallbackValue, StringFormat, TargetNullValue, BindingGroupName, or Delay after sealing raises this InvalidOperationException.","triggerScenarios":"Assigning Binding.FallbackValue, StringFormat, TargetNullValue, BindingGroupName, or Delay on a Binding after it has been attached to a DependencyProperty target (e.g. inside Loaded handler, after SetBinding, or from a value converter/multi-trigger seeing the live binding).","commonSituations":"Trying to tweak StringFormat at runtime for an already-bound control; sharing one Binding instance across code that keeps mutating it; dynamically changing Delay or BindingGroupName after window load.","solutions":["Clone the binding: create a new Binding with the new values and call SetBinding again","CloneBinding via BindingOperations (create a mutable copy) before changing properties","Configure all binding properties before assigning it to the target / before SetBinding"],"exampleFix":"// before\n((Binding)myText.GetBindingExpression(TextBlock.TextProperty).ParentBindingBase).StringFormat = \"C\"; // throws\n// after\nvar nb = new Binding(\"Price\") { StringFormat = \"C\" };\nmyText.SetBinding(TextBlock.TextProperty, nb);","handlingStrategy":"type-guard","validationCode":"if (((BindingBase)expr.ParentBindingBase).IsSealed) { /* clone instead of mutate */ }","typeGuard":"static bool IsMutable(BindingBase b) => b != null && !b.IsSealed;","tryCatchPattern":"try { binding.StringFormat = fmt; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"sealed\")) { var clone = binding.Clone(); clone.StringFormat = fmt; target.SetBinding(dp, clone); }","preventionTips":["Never mutate Binding objects after SetBinding; create new instances","Check IsSealed before property changes","Clone bindings (MemberwiseClone of a fresh Binding) for runtime reconfiguration"],"tags":["wpf","data-binding","sealed-binding","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}