dotnet/wpf · error · InvalidOperationException

SR.ChangeSealedBinding

Error message

SR.ChangeSealedBinding

What it means

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.

Solutions

  1. Clone the binding: create a new Binding with the new values and call SetBinding again
  2. CloneBinding via BindingOperations (create a mutable copy) before changing properties
  3. Configure all binding properties before assigning it to the target / before SetBinding

Example fix

// before
((Binding)myText.GetBindingExpression(TextBlock.TextProperty).ParentBindingBase).StringFormat = "C"; // throws
// after
var nb = new Binding("Price") { StringFormat = "C" };
myText.SetBinding(TextBlock.TextProperty, nb);
Defensive patterns

Strategy: type-guard

Validate before calling

if (((BindingBase)expr.ParentBindingBase).IsSealed) { /* clone instead of mutate */ }

Type guard

static bool IsMutable(BindingBase b) => b != null && !b.IsSealed;

Try / catch

try { binding.StringFormat = fmt; } catch (InvalidOperationException ex) when (ex.Message.Contains("sealed")) { var clone = binding.Clone(); clone.StringFormat = fmt; target.SetBinding(dp, clone); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d0db5a595347d5a9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingBase.cs:422

            _isSealed = true;
            return CreateBindingExpressionOverride(targetObject, targetProperty, null);
        }

        /// <summary>
        /// Create an appropriate expression for this Binding, to be attached
        /// to the given DependencyProperty on the given DependencyObject.
        /// </summary>
        internal BindingExpressionBase CreateBindingExpression(DependencyObject targetObject, DependencyProperty targetProperty, BindingExpressionBase owner)
        {
            _isSealed = true;
            return CreateBindingExpressionOverride(targetObject, targetProperty, owner);
        }

        // Throw if the binding is sealed.
        internal void CheckSealed()
        {
            if (_isSealed)
                throw new InvalidOperationException(SR.ChangeSealedBinding);
        }

        // Return one of the special ValidationRules
        internal ValidationRule GetValidationRule(Type type)
        {
            if (TestFlag(BindingFlags.ValidatesOnExceptions) && type == typeof(System.Windows.Controls.ExceptionValidationRule))
                return System.Windows.Controls.ExceptionValidationRule.Instance;

            if (TestFlag(BindingFlags.ValidatesOnDataErrors) && type == typeof(System.Windows.Controls.DataErrorValidationRule))
                return System.Windows.Controls.DataErrorValidationRule.Instance;

            if (TestFlag(BindingFlags.ValidatesOnNotifyDataErrors) && type == typeof(System.Windows.Controls.NotifyDataErrorValidationRule))
                return System.Windows.Controls.NotifyDataErrorValidationRule.Instance;

            return LookupValidationRule(type);
        }

        internal virtual ValidationRule LookupValidationRule(Type type)

View on GitHub (pinned to 81131a70a4)