{"record":{"id":"db9758cc211b8001","repo":"dotnet/wpf","slug":"sr-format-sr-readonlyoverridenotallowed-name","errorCode":null,"errorMessage":"SR.Format(SR.ReadOnlyOverrideNotAllowed, Name)","messagePattern":"SR\\.Format\\(SR\\.ReadOnlyOverrideNotAllowed, Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":504,"sourceCode":"\n        /// <summary>\n        ///     Supply metadata for given type & run static constructors if needed.\n        /// </summary>\n        /// <remarks>\n        ///     The supplied metadata will be merged with the type's base\n        ///     metadata\n        /// </remarks>\n        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata)\n        {\n            DependencyObjectType dType;\n            PropertyMetadata baseMetadata;\n\n            SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);\n\n            if (ReadOnly)\n            {\n                // Readonly and no DependencyPropertyKey - not allowed.\n                throw new InvalidOperationException(SR.Format(SR.ReadOnlyOverrideNotAllowed, Name));\n            }\n\n            ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);\n        }\n\n        /// <summary>\n        ///     Supply metadata for a given type, overriding a property that is\n        /// read-only.  If property is not read only, tells user to use the Plain\n        /// 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);","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L486-L522","documentation":"DependencyProperty.OverrideMetadata throws this InvalidOperationException when the property being overridden is read-only (ReadOnly is true) and the overload of OverrideMetadata without a DependencyPropertyKey is used. Read-only properties can only have their metadata overridden through the key-protected overload, to prevent arbitrary code from modifying metadata that only the property's definer should control.","triggerScenarios":"Calling dp.OverrideMetadata(forType, typeMetadata) (the two-argument public overload) where dp was registered via DependencyPropertyKey.DependencyProperty, i.e. ReadOnly == true.","commonSituations":"Subclassing a control and trying to override metadata (e.g. default value, FrameworkPropertyMetadataOptions) of a read-only property like UIElement.IsMouseOver or a custom read-only DP; copy-pasting override code from a writable DP to a read-only one.","solutions":["Use the key-protected overload: store the DependencyPropertyKey used at registration and call key.DependencyProperty.OverrideMetadata(forType, typeMetadata, key)","If you do not own the key (property defined in a type you cannot change), do not override the metadata; instead set the value via the key only from the defining type, or wrap with your own property","If the property should be writable, re-register it as a normal DependencyProperty instead of via DependencyPropertyKey"],"exampleFix":"// before\npublic class MyControl : Control\n{\n    static MyControl()\n    {\n        // IsMouseOverProperty is read-only\n        IsMouseOverProperty.OverrideMetadata(typeof(MyControl), new PropertyMetadata(false)); // throws\n    }\n}\n// after\npublic class MyControl : Control\n{\n    static MyControl()\n    {\n        // Only valid if you hold the key (owning type):\n        // ReadOnlyPropertyKey.OverrideMetadata(typeof(MyControl), new PropertyMetadata(false));\n        // or override using the key:\n        // MyReadOnlyProperty.OverrideMetadata(typeof(MyControl), meta, MyReadOnlyPropertyKey);\n    }\n}","handlingStrategy":"validation","validationCode":"if (dp.ReadOnly)\n    dp.OverrideMetadata(typeof(MyControl), meta, /* must supply the DependencyPropertyKey here */ key);\nelse\n    dp.OverrideMetadata(typeof(MyControl), meta);","typeGuard":"static bool CanOverrideWithoutKey(DependencyProperty dp) => !dp.ReadOnly;","tryCatchPattern":null,"preventionTips":["Remember: read-only DPs always require the key overload for metadata overrides","Keep DependencyPropertyKey fields accessible (internal) in the owning assembly for later overrides","Check dp.ReadOnly before any OverrideMetadata call"],"tags":["wpf","dependency-property","read-only","metadata-override","invalid-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}