{"record":{"id":"d1dc9374f4221d1b","repo":"AvaloniaUI/Avalonia","slug":"setter-value-value-is-not-a-valid-value-for-pr","errorCode":null,"errorMessage":"Setter value '{Value}' is not a valid value for property '{Property}'.","messagePattern":"Setter value '(.+?)' is not a valid value for property '(.+?)'\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/Setter.cs","lineNumber":87,"sourceCode":"        internal override ISetterInstance Instance(IStyleInstance instance, StyledElement target)\n        {\n            if (target is not AvaloniaObject ao)\n                throw new InvalidOperationException(\"Don't know how to instance a style on this type.\");\n            if (Property is null)\n                throw new InvalidOperationException(\"Setter.Property must be set.\");\n            if (Property.IsDirect && instance.HasActivator)\n                throw new InvalidOperationException(\n                    $\"Cannot set direct property '{Property}' in '{instance.Source}' because the style has an activator.\");\n            if (Property.IsClassesBindingProperty(out var classPropertyName) && instance.HasActivator)\n                throw new InvalidOperationException(\n                        $\"Cannot set Class Binding property '(Classes.{classPropertyName})' in '{instance.Source}' because the style has an activator.\");\n\n            if (Value is BindingBase binding)\n                return SetBinding((StyleInstance)instance, ao, binding);\n            else if (Value is ITemplate template && !typeof(ITemplate).IsAssignableFrom(Property.PropertyType))\n                return new PropertySetterTemplateInstance(Property, template);\n            else if (!Property.IsValidValue(Value))\n                throw new InvalidCastException($\"Setter value '{Value}' is not a valid value for property '{Property}'.\");\n            else if (Property.IsDirect)\n                return SetDirectValue(target);\n            else\n                return this;\n        }\n\n        bool IValueEntry.HasValue() => true;\n        object? IValueEntry.GetValue() => Value;\n\n        bool IValueEntry.GetDataValidationState(out BindingValueType state, out Exception? error)\n        {\n            state = BindingValueType.Value;\n            error = null;\n            return false;\n        }\n\n        private AvaloniaProperty EnsureProperty()\n        {","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/Setter.cs#L69-L105","documentation":"Thrown as InvalidCastException by Setter.Instance when Value fails Property.IsValidValue(Value) — i.e. the supplied value's type or content is not acceptable for the target AvaloniaProperty. Unlike a type cast, IsValidValue enforces validation rules, coercion readiness, and type compatibility. The message shows both the offending value and the property.","triggerScenarios":"Setting a Setter Value to an object whose type does not match the property (e.g. a string for a Brush property without a converter), or a value that violates the property's validation callback. Common with AXAML string values for non-string properties where no type converter is registered.","commonSituations":"AXAML `<Setter Property=\"Foreground\" Value=\"NotAColor\"/>`; passing a raw string to a property expecting a typed object in C#; enum/int mismatches; null for a non-nullable property.","solutions":["Provide a value of the property's declared type (e.g. use Brushes.Red or a StaticResource/SolidColorBrush instead of a literal for Brush properties).","In AXAML, ensure a type converter exists for the string-to-property conversion, or use `{StaticResource}`/`{x:Static}`.","Check the property's registered validation via IsValidValue in a test before assigning."],"exampleFix":"// before\nnew Setter(Control.BackgroundProperty, \"Red\") // string not valid for IBrush\n\n// after\nnew Setter(Control.BackgroundProperty, Brushes.Red)","handlingStrategy":"validation","validationCode":"foreach (var setter in style.Setters.OfType<Setter>())\n    if (setter.Property is not null && !setter.Property.IsValidValue(setter.Value))\n        throw new InvalidCastException($\"Value {setter.Value} is invalid for {setter.Property}.\");","typeGuard":"static bool ValueIsValid(AvaloniaProperty prop, object? value) => prop.IsValidValue(value);","tryCatchPattern":null,"preventionTips":["Provide values of the property's declared type.","Use brushes/resources for Brush properties rather than raw strings in C#.","Call IsValidValue in tests when building setters dynamically."],"tags":["avalonia","styling","setter","value-validation","type-mismatch"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}