{"record":{"id":"0300d45d1b9045ff","repo":"dotnet/wpf","slug":"sr-readonlykeynotauthorized","errorCode":null,"errorMessage":"SR.ReadOnlyKeyNotAuthorized","messagePattern":"SR\\.ReadOnlyKeyNotAuthorized","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":929,"sourceCode":"\n        /// <summary>\n        ///     Returns the DependencyPropertyKey associated with this DP.\n        /// </summary>\n        internal DependencyPropertyKey DependencyPropertyKey\n        {\n            get\n            {\n                return _readOnlyKey;\n            }\n        }\n\n        internal void VerifyReadOnlyKey( DependencyPropertyKey candidateKey )\n        {\n            Debug.Assert( ReadOnly, \"Why are we trying to validate read-only key on a property that is not read-only?\");\n\n            if (_readOnlyKey != candidateKey)\n            {\n                throw new ArgumentException(SR.ReadOnlyKeyNotAuthorized);\n            }\n        }\n\n        /// <summary>\n        ///     Internal version of IsValidValue that bypasses IsValidType check;\n        ///     Called from SetValueInternal\n        /// </summary>\n        /// <param name=\"value\">Value to check</param>\n        /// <returns>true if value is appropriate</returns>\n        internal bool IsValidValueInternal(object value)\n        {\n            if (ValidateValueCallback != null)\n            {\n                // CALLBACK\n                return ValidateValueCallback(value);\n            }\n\n            return true;","sourceCodeStart":911,"sourceCodeEnd":947,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L911-L947","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the exact static DependencyPropertyKey returned from DependencyProperty.RegisterReadOnly for that property","Expose the key as a private/internal static field on the defining type and use it consistently","Confirm key.DependencyProperty == the target DP before calling OverrideMetadata"],"exampleFix":"// before\nprivate static readonly DependencyPropertyKey OtherKey = DependencyProperty.RegisterReadOnly(\"Other\", ...);\nMyProp.OverrideMetadata(typeof(C), meta, OtherKey); // wrong key instance\n// after\nprivate static readonly DependencyPropertyKey MyPropKey = DependencyProperty.RegisterReadOnly(\"MyProp\", ...);\nMyProp.OverrideMetadata(typeof(C), meta, MyPropKey);","handlingStrategy":"validation","validationCode":"if (key.DependencyProperty != readOnlyDp)\n    throw new InvalidOperationException(\"Key does not belong to \" + readOnlyDp.Name);","typeGuard":null,"tryCatchPattern":"try { readOnlyDp.OverrideMetadata(forType, meta, key); }\ncatch (ArgumentException) { /* unauthorized key: fall back to the original static key field */ }","preventionTips":["Always use the exact key instance returned from RegisterReadOnly","Keep keys private to the defining type; pass the DP (key.DependencyProperty) outward instead","Never fabricate or copy DependencyPropertyKey instances"],"tags":["wpf","dependency-property","read-only","wrong-key","authorization"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}