{"record":{"id":"949c42911d62b9fa","repo":"dotnet/wpf","slug":"sr-format-sr-readonlyoverridekeynotauthorized-name","errorCode":null,"errorMessage":"SR.Format(SR.ReadOnlyOverrideKeyNotAuthorized, Name)","messagePattern":"SR\\.Format\\(SR\\.ReadOnlyOverrideKeyNotAuthorized, Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":531,"sourceCode":"        /// Jane OverrideMetadata instead.\n        /// </summary>\n        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata, DependencyPropertyKey key)\n        {\n            DependencyObjectType dType;\n            PropertyMetadata baseMetadata;\n\n            SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);\n\n            ArgumentNullException.ThrowIfNull(key);\n\n            if (ReadOnly)\n            {\n                // If the property is read-only, the key must match this property\n                //  and the key must match that in the base metadata.\n\n                if (key.DependencyProperty != this)\n                {\n                    throw new ArgumentException(SR.Format(SR.ReadOnlyOverrideKeyNotAuthorized, Name));\n                }\n\n                VerifyReadOnlyKey(key);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.PropertyNotReadOnly);\n            }\n\n            // Either the property doesn't require a key, or the key match was\n            //  successful.  Proceed with the metadata override.\n            ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);\n        }\n\n        /// <summary>\n        ///     After parameters have been validated for OverrideMetadata, this\n        /// method is called to actually update the data structures.\n        /// </summary>","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L513-L549","documentation":"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.","triggerScenarios":"Calling readOnlyDp.OverrideMetadata(forType, typeMetadata, key) where key is a DependencyPropertyKey belonging to a different read-only property.","commonSituations":"Copy-pasting override code between read-only properties; mixing up two similar DependencyPropertyKey static fields; refactoring renamed keys so the wrong one is passed.","solutions":["Pass the DependencyPropertyKey whose DependencyProperty property equals the DP being overridden (usually the same static key used at RegisterReadOnly)","Check key.DependencyProperty == dp before calling to verify the right key","If overriding several read-only properties, make sure each call uses its own matching key"],"exampleFix":"// before\nValueProperty.OverrideMetadata(typeof(MyControl), meta, OtherPropertyKey); // wrong key\n// after\nValueProperty.OverrideMetadata(typeof(MyControl), meta, ValuePropertyKey); // key whose .DependencyProperty == ValueProperty","handlingStrategy":"validation","validationCode":"if (key.DependencyProperty != dp)\n    throw new InvalidOperationException(\"Wrong DependencyPropertyKey for \" + dp.Name);\ndp.OverrideMetadata(forType, meta, key);","typeGuard":null,"tryCatchPattern":"try { dp.OverrideMetadata(forType, meta, key); }\ncatch (ArgumentException ex) { /* key/property mismatch: log and use the key stored next to the DP definition */ }","preventionTips":["Store each DependencyPropertyKey as a static readonly field immediately beside its DP","Never pass another property's key; each override call cites its own key","Assert key.DependencyProperty == dp in debug builds"],"tags":["wpf","dependency-property","read-only","wrong-key","argument"],"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"}