dotnet/wpf · error · InvalidOperationException

SR.TypeMetadataCannotChangeAfterUse

Error message

SR.TypeMetadataCannotChangeAfterUse

What it means

UIPropertyMetadata.IsAnimationProhibited (and other metadata settings) can only be changed while the metadata instance is unsealed. Metadata is sealed once attached to a DependencyProperty via OverrideMetadata/AddOwner, after which any setter throws InvalidOperationException(SR.TypeMetadataCannotChangeAfterUse).

Solutions

  1. Create a new UIPropertyMetadata instance and call OverrideMetadata again instead of mutating the sealed one
  2. Set IsAnimationProhibited before the metadata is attached to a property (at type-initialization time)
  3. Check the Sealed property before writing metadata flags

Example fix

// before
myMetadata.IsAnimationProhibited = true; // metadata already sealed
// after
var newMetadata = new UIPropertyMetadata(myMetadata.DefaultValue) { IsAnimationProhibited = true };
MyProperty.OverrideMetadata(typeof(MyControl), newMetadata);
Defensive patterns

Strategy: validation

Validate before calling

if (!myMetadata.Sealed) { myMetadata.IsAnimationProhibited = value; }
else { /* create a fresh UIPropertyMetadata and OverrideMetadata */ }

Type guard

static bool CanMutate(UIPropertyMetadata m) => !m.Sealed;

Try / catch

try { metadata.IsAnimationProhibited = true; }
catch (InvalidOperationException ex) when (ex.Message.Contains("metadata")) { log.Error("Metadata already sealed; create a new instance", ex); }

Prevention

When it happens

Trigger: Setting IsAnimationProhibited on a metadata instance that was already passed to OverrideMetadata or otherwise used to register/override a property.

Common situations: Tweaking shared metadata instances after framework startup; mutating a base class's metadata instance instead of creating a new one; changing metadata in response to runtime conditions.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIPropertyMetadata.cs:106

        /// <summary>
        /// Set this to true for a property for which animation should be
        /// prohibited. This should not be set unless there are very strong
        /// technical reasons why a property can not be animated. In the
        /// vast majority of cases, a property that can not be properly
        /// animated means that the property implementation contains a bug.
        /// </summary>
        public bool IsAnimationProhibited
        {
            get
            {
                return ReadFlag(MetadataFlags.UI_IsAnimationProhibitedID);
            }
            set
            {
                if (Sealed)
                {
                    throw new InvalidOperationException(SR.TypeMetadataCannotChangeAfterUse);
                }

                WriteFlag(MetadataFlags.UI_IsAnimationProhibitedID, value);
            }
        }
    }
}

View on GitHub (pinned to 81131a70a4)