dotnet/wpf · error · ArgumentException

SR.ReadOnlyKeyNotAuthorized

Error message

SR.ReadOnlyKeyNotAuthorized

What it means

VerifyReadOnlyKey throws this ArgumentException when the candidateKey passed for a read-only DependencyProperty is not the exact _readOnlyKey instance created at RegisterReadOnly time. Only the original key authorizes value writes and metadata overrides on the read-only property.

Solutions

  1. Pass the exact static DependencyPropertyKey returned from DependencyProperty.RegisterReadOnly for that property
  2. Expose the key as a private/internal static field on the defining type and use it consistently
  3. Confirm key.DependencyProperty == the target DP before calling OverrideMetadata

Example fix

// before
private static readonly DependencyPropertyKey OtherKey = DependencyProperty.RegisterReadOnly("Other", ...);
MyProp.OverrideMetadata(typeof(C), meta, OtherKey); // wrong key instance
// after
private static readonly DependencyPropertyKey MyPropKey = DependencyProperty.RegisterReadOnly("MyProp", ...);
MyProp.OverrideMetadata(typeof(C), meta, MyPropKey);
Defensive patterns

Strategy: validation

Validate before calling

if (key.DependencyProperty != readOnlyDp)
    throw new InvalidOperationException("Key does not belong to " + readOnlyDp.Name);

Try / catch

try { readOnlyDp.OverrideMetadata(forType, meta, key); }
catch (ArgumentException) { /* unauthorized key: fall back to the original static key field */ }

Prevention

When it happens

Trigger: Calling readOnlyDp.VerifyReadOnlyKey indirectly via SetupPropertyChange or OverrideMetadata(forType, meta, key) with a key that is a different DependencyPropertyKey instance — e.g. a key created for another property, or a newly constructed/copy of a key.

Common situations: Passing one read-only property's key to another; constructing a fake key via serialization/reflection; refactor created two keys for what used to be one property.

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

Appendix: source

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

        /// <summary>
        ///     Returns the DependencyPropertyKey associated with this DP.
        /// </summary>
        internal DependencyPropertyKey DependencyPropertyKey
        {
            get
            {
                return _readOnlyKey;
            }
        }

        internal void VerifyReadOnlyKey( DependencyPropertyKey candidateKey )
        {
            Debug.Assert( ReadOnly, "Why are we trying to validate read-only key on a property that is not read-only?");

            if (_readOnlyKey != candidateKey)
            {
                throw new ArgumentException(SR.ReadOnlyKeyNotAuthorized);
            }
        }

        /// <summary>
        ///     Internal version of IsValidValue that bypasses IsValidType check;
        ///     Called from SetValueInternal
        /// </summary>
        /// <param name="value">Value to check</param>
        /// <returns>true if value is appropriate</returns>
        internal bool IsValidValueInternal(object value)
        {
            if (ValidateValueCallback != null)
            {
                // CALLBACK
                return ValidateValueCallback(value);
            }

            return true;

View on GitHub (pinned to 81131a70a4)