{"record":{"id":"eddd5f235b025379","repo":"dotnet/wpf","slug":"sr-readonlychangenotallowed","errorCode":null,"errorMessage":"SR.ReadOnlyChangeNotAllowed","messagePattern":"SR\\.ReadOnlyChangeNotAllowed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyObject.cs","lineNumber":590,"sourceCode":"            SetValueCommon(dp, value, metadata, coerceWithDeferredReference: false, coerceWithCurrentValue: false, OperationType.Unknown, isInternal: false);\n        }\n\n        /// <summary>\n        ///     Called by SetValue or ClearValue to verify that the property\n        /// can be changed.\n        /// </summary>\n        private PropertyMetadata SetupPropertyChange(DependencyProperty dp)\n        {\n            ArgumentNullException.ThrowIfNull(dp);\n\n            if (!dp.ReadOnly)\n            {\n                // Get type-specific metadata for this property\n                return dp.GetMetadata(DependencyObjectType);\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.ReadOnlyChangeNotAllowed, dp.Name));\n            }\n        }\n\n        /// <summary>\n        ///     Called by SetValue or ClearValue to verify that the property\n        /// can be changed.\n        /// </summary>\n        private PropertyMetadata SetupPropertyChange(DependencyPropertyKey key, out DependencyProperty dp)\n        {\n            ArgumentNullException.ThrowIfNull(key);\n\n            dp = key.DependencyProperty;\n            Debug.Assert(dp != null);\n\n            dp.VerifyReadOnlyKey(key);\n\n            // Get type-specific metadata for this property\n            return dp.GetMetadata(DependencyObjectType);","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyObject.cs#L572-L608","documentation":"DependencyObject.SetupPropertyChange (used by SetValue/ClearValue via CheckReadOnly) throws InvalidOperationException(SR.ReadOnlyChangeNotAllowed, dp.Name) when the target DependencyProperty is read-only (ReadOnly=true, registered via DependencyPropertyKey). Read-only DPs (e.g. many system/modeled properties like IsMouseOver) can only be set through their internal key.","triggerScenarios":"Calling SetValue(SomeReadOnlyProperty, value) or ClearValue(SomeReadOnlyProperty) on properties registered read-only, e.g. attempting UIElement.IsMouseOver = true or writing to a read-only DP you registered yourself with AddOwner without the key.","commonSituations":"Trying to simulate mouse states in tests; writing to read-only DPs exposed by third-party controls; reflection-based property assignment enumerating all DPs including read-only ones.","solutions":["Use the DependencyPropertyKey (if you own the property or it's exposed via internal API) instead of the public DP.","Check dp.ReadOnly before SetValue/ClearValue and skip read-only properties.","For state simulation, use the intended mechanism (e.g. RaiseEvent for mouse, or a TestHook) rather than writing read-only DPs."],"exampleFix":"// before\nif (property.IsMouseOverProperty != null)\n    element.SetValue(UIElement.IsMouseOverProperty, true);\n// after\nif (!UIElement.IsMouseOverProperty.ReadOnly)\n    element.SetValue(UIElement.IsMouseOverProperty, true);\nelse\n    /* simulate via input events */;","handlingStrategy":"type-guard","validationCode":"if (dp == null || dp.ReadOnly)\n    return; // skip read-only dependency properties","typeGuard":"bool IsWritable(DependencyProperty dp) => dp != null && !dp.ReadOnly;","tryCatchPattern":"try { obj.SetValue(dp, value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(dp.Name))\n{\n    log.LogWarning($\"{dp.Name} is read-only; use its DependencyPropertyKey or events instead.\");\n}","preventionTips":["Check dp.ReadOnly before any SetValue/ClearValue loop.","Never attempt to fake UI state by writing read-only DPs; simulate input events instead.","When exposing read-only DPs yourself, also expose a method that uses the DependencyPropertyKey."],"tags":["wpf","dependency-property","read-only","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"}