dotnet/wpf · error · ArgumentException

SR.Format(SR.ReadOnlyOverrideKeyNotAuthorized, Name)

Error message

SR.Format(SR.ReadOnlyOverrideKeyNotAuthorized, Name)

What it means

When overriding metadata on a read-only DependencyProperty with the key-protected overload, the supplied DependencyPropertyKey must be the key for THIS property. If key.DependencyProperty != this, ArgumentException is thrown because the key was issued for a different property and therefore does not authorize a metadata override.

Solutions

  1. Pass the DependencyPropertyKey whose DependencyProperty property equals the DP being overridden (usually the same static key used at RegisterReadOnly)
  2. Check key.DependencyProperty == dp before calling to verify the right key
  3. If overriding several read-only properties, make sure each call uses its own matching key

Example fix

// before
ValueProperty.OverrideMetadata(typeof(MyControl), meta, OtherPropertyKey); // wrong key
// after
ValueProperty.OverrideMetadata(typeof(MyControl), meta, ValuePropertyKey); // key whose .DependencyProperty == ValueProperty
Defensive patterns

Strategy: validation

Validate before calling

if (key.DependencyProperty != dp)
    throw new InvalidOperationException("Wrong DependencyPropertyKey for " + dp.Name);
dp.OverrideMetadata(forType, meta, key);

Try / catch

try { dp.OverrideMetadata(forType, meta, key); }
catch (ArgumentException ex) { /* key/property mismatch: log and use the key stored next to the DP definition */ }

Prevention

When it happens

Trigger: Calling readOnlyDp.OverrideMetadata(forType, typeMetadata, key) where key is a DependencyPropertyKey belonging to a different read-only property.

Common situations: Copy-pasting override code between read-only properties; mixing up two similar DependencyPropertyKey static fields; refactoring renamed keys so the wrong one is passed.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs:531

        /// Jane OverrideMetadata instead.
        /// </summary>
        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata, DependencyPropertyKey key)
        {
            DependencyObjectType dType;
            PropertyMetadata baseMetadata;

            SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);

            ArgumentNullException.ThrowIfNull(key);

            if (ReadOnly)
            {
                // If the property is read-only, the key must match this property
                //  and the key must match that in the base metadata.

                if (key.DependencyProperty != this)
                {
                    throw new ArgumentException(SR.Format(SR.ReadOnlyOverrideKeyNotAuthorized, Name));
                }

                VerifyReadOnlyKey(key);
            }
            else
            {
                throw new InvalidOperationException(SR.PropertyNotReadOnly);
            }

            // Either the property doesn't require a key, or the key match was
            //  successful.  Proceed with the metadata override.
            ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);
        }

        /// <summary>
        ///     After parameters have been validated for OverrideMetadata, this
        /// method is called to actually update the data structures.
        /// </summary>

View on GitHub (pinned to 81131a70a4)