{"record":{"id":"5b0f86d92762171a","repo":"dotnet/wpf","slug":"sr-format-sr-readonlypropertynotallowed-dp-name-gettype-name","errorCode":null,"errorMessage":"SR.Format(SR.ReadOnlyPropertyNotAllowed, dp.Name, GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.ReadOnlyPropertyNotAllowed, dp\\.Name, GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":222,"sourceCode":"            // Value needs to be valid for the DP, or Binding/MultiBinding/PriorityBinding.\n            //  (They all have MarkupExtension, which we don't actually support, see above check.)\n\n            if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidPropertyValue, value, dp.Name));\n            }\n\n            // Styling the logical tree is not supported\n            if (StyleHelper.IsStylingLogicalTree(dp, value))\n            {\n                throw new NotSupportedException(SR.Format(SR.ModifyingLogicalTreeViaStylesNotImplemented, value, \"FrameworkElementFactory.SetValue\"));\n            }\n\n            if (dp.ReadOnly)\n            {\n                // Read-only properties will not be consulting FrameworkElementFactory for value.\n                //  Rather than silently do nothing, throw error.\n                throw new ArgumentException(SR.Format(SR.ReadOnlyPropertyNotAllowed, dp.Name, GetType().Name));\n            }\n\n            ResourceReferenceExpression resourceExpression = value as ResourceReferenceExpression;\n            DynamicResourceExtension dynamicResourceExtension = value as DynamicResourceExtension;\n            object resourceKey = null;\n\n            if( resourceExpression != null )\n            {\n                resourceKey = resourceExpression.ResourceKey;\n            }\n            else if( dynamicResourceExtension != null )\n            {\n                resourceKey = dynamicResourceExtension.ResourceKey;\n            }\n\n            if (resourceKey == null)\n            {\n                TemplateBindingExtension templateBinding = value as TemplateBindingExtension;","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L204-L240","documentation":"FrameworkElementFactory.SetValue throws ArgumentException when the dependency property is read-only (dp.ReadOnly). Read-only properties are never consulted through the factory because their values are computed by the framework, so rather than silently ignoring the call WPF throws. The message names the property and the factory type.","triggerScenarios":"Calling factory.SetValue(SomeReadOnlyProperty, value), e.g. setting properties registered with DependencyProperty.RegisterReadOnly such as ActualWidth-like or ItemsSource-derived DPs.","commonSituations":"Copy-pasting SetValue calls from element code into factory code without noticing the target DP is read-only; attempting to 'pre-set' a computed property on template children.","solutions":["Remove the SetValue call for the read-only property.","If you need to influence the value, set the underlying input property (the one the read-only DP is computed from) instead.","If a settable equivalent exists (non-readonly registration), target that property."],"exampleFix":"// before\nfactory.SetValue(TextBox.IsReadOnlyPropertyCoreReadOnly, true);\n// after\nfactory.SetValue(TextBox.IsReadOnlyProperty, true); // settable DP","handlingStrategy":"validation","validationCode":"if (dp.ReadOnly) throw new InvalidOperationException($\"{dp.Name} is read-only; remove the SetValue call.\");","typeGuard":"static bool IsSettable(DependencyProperty dp) => !dp.ReadOnly;","tryCatchPattern":"try { factory.SetValue(dp, value); }\ncatch (ArgumentException ex) when (dp.ReadOnly) { log.LogWarning(ex.Message); }","preventionTips":["Check dp.ReadOnly before calling SetValue.","Review read-only DP names (RegisterReadOnly) when porting element code to factory code.","Set the upstream input property instead of the computed one."],"tags":["wpf","dependency-property","read-only"],"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"}