{"record":{"id":"eb75acc4d84aea40","repo":"dotnet/wpf","slug":"sr-format-sr-readonlydesignercoersionnotallowed-name","errorCode":null,"errorMessage":"SR.Format(SR.ReadOnlyDesignerCoersionNotAllowed, Name)","messagePattern":"SR\\.Format\\(SR\\.ReadOnlyDesignerCoersionNotAllowed, Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":1125,"sourceCode":"\n            // Covered by Synchronized by caller\n            return GlobalIndexCount++;\n        }\n\n        /// <summary>\n        /// This is the callback designers use to participate in the computation of property\n        /// values at design time. Eg. Even if the author sets Visibility to Hidden, the designer\n        /// wants to coerce the value to Visible at design time so that the element doesn't\n        /// disappear from the design surface.\n        /// </summary>\n        internal CoerceValueCallback DesignerCoerceValueCallback\n        {\n            get {  return _designerCoerceValueCallback; }\n            set\n            {\n                if (ReadOnly)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.ReadOnlyDesignerCoersionNotAllowed, Name));\n                }\n\n                _designerCoerceValueCallback = value;\n            }\n        }\n\n        /// <summary> Standard unset value </summary>\n        public static readonly object UnsetValue = new NamedObject(\"DependencyProperty.UnsetValue\");\n\n        private string _name;\n        private Type _propertyType;\n        private Type _ownerType;\n        private PropertyMetadata _defaultMetadata;\n        private ValidateValueCallback _validateValueCallback;\n        private DependencyPropertyKey _readOnlyKey;\n\n\n        [Flags]","sourceCodeStart":1107,"sourceCodeEnd":1143,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L1107-L1143","documentation":"The DesignerCoerceValueCallback property setter throws this InvalidOperationException when the DependencyProperty is read-only. A designer-supplied coercion callback would allow mutating the value of a read-only property, which the key-based security model forbids, so the setter is blocked entirely.","triggerScenarios":"Assigning dp.DesignerCoerceValueCallback = callback where dp is a read-only property (registered via DependencyPropertyKey), typically from designer tooling or custom property-system extensions.","commonSituations":"Visual Studio designer or third-party designer extensions trying to install coercion on read-only DPs like IsMouseOver; test harnesses poking at the property-system extension points.","solutions":["Do not set DesignerCoerceValueCallback on read-only properties; coerce at the source that writes via the key instead","If you own the property and need coercion, re-register it as writable or apply coercion logic inside the code that sets the value through the DependencyPropertyKey","Only attach DesignerCoerceValueCallback to writable DPs, guarded by if (!dp.ReadOnly)"],"exampleFix":"// before\nif (needsCoercion)\n    someDp.DesignerCoerceValueCallback = CoerceValue; // throws for read-only DPs\n// after\nif (needsCoercion && !someDp.ReadOnly)\n    someDp.DesignerCoerceValueCallback = CoerceValue;\n// for read-only DPs, coerce inside the key-protected setter path instead","handlingStrategy":"validation","validationCode":"if (!dp.ReadOnly)\n    dp.DesignerCoerceValueCallback = CoerceValue;\nelse\n    ApplyCoercionAtWriteSite();","typeGuard":"static bool AcceptsDesignerCoercion(DependencyProperty dp) => !dp.ReadOnly;","tryCatchPattern":null,"preventionTips":["Never attach designer callbacks to read-only DPs","Coerce read-only values in the code that writes via the key","Guard extension code with if (!dp.ReadOnly)"],"tags":["wpf","dependency-property","read-only","designer-callback","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-21T21:30:21.729Z"}