AvaloniaUI/Avalonia · error · InvalidCastException

Setter value '{Value}' is not a valid value for property '{P

Error message

Setter value '{Value}' is not a valid value for property '{Property}'.

What it means

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.

Source

Thrown at src/Avalonia.Base/Styling/Setter.cs:87

        internal override ISetterInstance Instance(IStyleInstance instance, StyledElement target)
        {
            if (target is not AvaloniaObject ao)
                throw new InvalidOperationException("Don't know how to instance a style on this type.");
            if (Property is null)
                throw new InvalidOperationException("Setter.Property must be set.");
            if (Property.IsDirect && instance.HasActivator)
                throw new InvalidOperationException(
                    $"Cannot set direct property '{Property}' in '{instance.Source}' because the style has an activator.");
            if (Property.IsClassesBindingProperty(out var classPropertyName) && instance.HasActivator)
                throw new InvalidOperationException(
                        $"Cannot set Class Binding property '(Classes.{classPropertyName})' in '{instance.Source}' because the style has an activator.");

            if (Value is BindingBase binding)
                return SetBinding((StyleInstance)instance, ao, binding);
            else if (Value is ITemplate template && !typeof(ITemplate).IsAssignableFrom(Property.PropertyType))
                return new PropertySetterTemplateInstance(Property, template);
            else if (!Property.IsValidValue(Value))
                throw new InvalidCastException($"Setter value '{Value}' is not a valid value for property '{Property}'.");
            else if (Property.IsDirect)
                return SetDirectValue(target);
            else
                return this;
        }

        bool IValueEntry.HasValue() => true;
        object? IValueEntry.GetValue() => Value;

        bool IValueEntry.GetDataValidationState(out BindingValueType state, out Exception? error)
        {
            state = BindingValueType.Value;
            error = null;
            return false;
        }

        private AvaloniaProperty EnsureProperty()
        {

View on GitHub (pinned to 11c5427268)

Solutions

  1. 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).
  2. In AXAML, ensure a type converter exists for the string-to-property conversion, or use `{StaticResource}`/`{x:Static}`.
  3. Check the property's registered validation via IsValidValue in a test before assigning.

Example fix

// before
new Setter(Control.BackgroundProperty, "Red") // string not valid for IBrush

// after
new Setter(Control.BackgroundProperty, Brushes.Red)
Defensive patterns

Strategy: validation

Validate before calling

foreach (var setter in style.Setters.OfType<Setter>())
    if (setter.Property is not null && !setter.Property.IsValidValue(setter.Value))
        throw new InvalidCastException($"Value {setter.Value} is invalid for {setter.Property}.");

Type guard

static bool ValueIsValid(AvaloniaProperty prop, object? value) => prop.IsValidValue(value);

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/d1dc9374f4221d1b. Report an issue: GitHub.