dotnet/wpf · error · InvalidOperationException

SR.CannotChangeAfterSealed

Error message

SR.CannotChangeAfterSealed

What it means

Condition.Property cannot be modified after the Condition has been sealed. Conditions are sealed when their owning Trigger/Style is applied/sealed, so changing Property afterwards throws InvalidOperationException (CannotChangeAfterSealed).

Solutions

  1. Create a new Condition (and new Trigger) instead of mutating a sealed one
  2. Modify conditions only before the Style/Trigger is sealed (before ApplyTemplate/Style application)
  3. Rebuild the whole Style and re-apply it if dynamic behavior is needed

Example fix

// before
sealedCondition.Property = SomeProperty; // throws
// after
var condition = new Condition(SomeProperty, value);
trigger.Conditions.Add(condition);
Defensive patterns

Strategy: validation

Validate before calling

// Only mutate conditions before the owning Style/Trigger is applied; else recreate
var condition = new Condition(newProperty, conditionValue);
trigger.Conditions.Add(condition);

Try / catch

try { condition.Property = p; } catch (InvalidOperationException) { /* recreate condition + trigger */ }

Prevention

When it happens

Trigger: Setting the Property property of a Condition that belongs to a trigger already sealed by an applied Style/Template.

Common situations: Mutating trigger conditions at runtime after the style was applied; reusing a cached trigger object across styles and trying to reconfigure it.

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/7273350b8fb3ef79. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Condition.cs:83

            ArgumentNullException.ThrowIfNull(binding);

            Binding = binding;
            Value  = conditionValue;
        }

        /// <summary>
        ///     DepedencyProperty of the conditional
        /// </summary>
        [Ambient]
        [DefaultValue(null)]
        public DependencyProperty Property
        {
            get { return _property; }
            set
            {
                if (_sealed)
                {
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "Condition"));
                }

                if (_binding != null)
                {
                    throw new InvalidOperationException(SR.ConditionCannotUseBothPropertyAndBinding);
                }

                _property = value;
            }
        }

        /// <summary>
        ///     Binding of the conditional
        /// </summary>
        [DefaultValue(null)]
        public BindingBase Binding
        {
            get { return _binding; }

View on GitHub (pinned to 81131a70a4)