{"record":{"id":"07157dae41d848fa","repo":"dotnet/wpf","slug":"cannot-set-read-only-property-0","errorCode":null,"errorMessage":"Cannot set read-only property '{0}'.","messagePattern":"Cannot set read-only property '(.+?)'\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Runtime/DynamicMethodRuntime.cs","lineNumber":261,"sourceCode":"                throw new NotSupportedException(SR.Format(SR.CantGetWriteonlyProperty, member));\n            }\n\n            PropertyGetDelegate getterDelegate;\n            if (!PropertyGetDelegates.TryGetValue(getter, out getterDelegate))\n            {\n                getterDelegate = CreateGetDelegate(getter);\n                PropertyGetDelegates.Add(getter, getterDelegate);\n            }\n\n            return getterDelegate.Invoke(obj);\n        }\n\n        protected override void SetValue(XamlMember member, object obj, object value)\n        {\n            MethodInfo setter = member.Invoker.UnderlyingSetter;\n            if (setter is null)\n            {\n                throw new NotSupportedException(SR.Format(SR.CantSetReadonlyProperty, member));\n            }\n\n            PropertySetDelegate setterDelegate;\n            if (!PropertySetDelegates.TryGetValue(setter, out setterDelegate))\n            {\n                setterDelegate = CreateSetDelegate(setter);\n                PropertySetDelegates.Add(setter, setterDelegate);\n            }\n\n            setterDelegate.Invoke(obj, value);\n        }\n\n        private DelegateCreator CreateDelegateCreator(Type targetType)\n        {\n            const BindingFlags helperFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;\n\n            // We are relying on WPF-generated delegate helper for now\n            // Expected signature: internal Delegate _CreateDelegate(Type delegateType, string handler)","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Runtime/DynamicMethodRuntime.cs#L243-L279","documentation":"DynamicMethodRuntime.SetValue is called to assign a member's value, but the member's invoker has no UnderlyingSetter (the property is read-only). It throws NotSupportedException with CantSetReadonlyProperty ('Cannot set a read-only property {0}'). The library throws this because there is no setter method to invoke.","triggerScenarios":"XAML load attempts to set a member whose CLR property has no setter (get-only, or get-only collection property whose collection cannot accept items via add), or markup assigns a value to a read-only property like FrameworkElement.ActualWidth.","commonSituations":"Setting built-in read-only WPF properties (ActualWidth, Templatized values) in XAML; view-model properties exposed read-only but referenced by bindings/styles in parsed XAML; collection properties declared without a setter and with non-addable collections.","solutions":["Remove the attribute/element that assigns the read-only property from the XAML","Add a setter to the property if it should be settable from XAML","For get-only collection properties, ensure the collection itself supports Add so the item elements can be appended instead of assigning the property"],"exampleFix":"// before\n<Label ActualWidth=\"200\" />  <!-- ActualWidth is read-only -->\n// after\n<Label Width=\"200\" />","handlingStrategy":"try-catch","validationCode":"static void EnsureSettable(Type t, string prop) { var p = t.GetProperty(prop); if (p != null && p.SetMethod == null) throw new InvalidOperationException($\"{t}.{prop} is read-only and cannot be set from XAML\"); }","typeGuard":"static bool IsXamlSettable(System.Reflection.PropertyInfo p) => p?.SetMethod is { IsPublic: true };","tryCatchPattern":"try { Load(xaml); } catch (NotSupportedException ex) when (ex.Message.Contains(\"read-only property\")) { throw new InvalidOperationException($\"Remove assignment to read-only member: {ex.Message}\", ex); }","preventionTips":["Never assign computed properties (ActualWidth, ActualHeight) in XAML","Give collection properties get-only setters with addable collection types","Keep view-model properties bindable-but-settable if XAML must write them"],"tags":["xaml","property","read-only","runtime"],"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"}