AvaloniaUI/Avalonia · error · InvalidOperationException
Setter.Property must be set.
Error message
Setter.Property must be set.
What it means
Thrown by Setter.Instance when the Setter's Property is null at the time the style is attached. A Setter must reference a valid AvaloniaProperty to apply; a null Property means the setter was declared without specifying what to set. This fires during style attachment (late, at apply time).
Source
Thrown at src/Avalonia.Base/Styling/Setter.cs:74
{
(value as ISetterValue)?.Initialize(this);
_value = value;
}
}
AvaloniaProperty IValueEntry.Property => EnsureProperty();
public override string ToString() => $"Setter: {Property} = {Value}";
void IValueEntry.Unsubscribe() { }
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = TrimmingMessages.ImplicitTypeConversionSupressWarningMessage)]
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;
}View on GitHub (pinned to 11c5427268)
Solutions
- Set Property on every Setter, e.g. `<Setter Property="Background" Value="Red"/>` or `new Setter(Control.BackgroundProperty, Brushes.Red)`.
- Audit AXAML for `<Setter>` elements lacking a Property attribute.
- If constructing in C#, use the (property, value) constructor to guarantee both are set.
Example fix
// before
style.Setters.Add(new Setter { Value = Brushes.Red });
// after
style.Setters.Add(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 null)
throw new InvalidOperationException($"Setter is missing Property (value={setter.Value})."); Type guard
static bool HasProperty(Setter s) => s.Property is not null;
Prevention
- Prefer the Setter(property, value) constructor to force setting Property.
- Lint AXAML for `<Setter>` elements missing the Property attribute.
- Validate Setters collection before attaching a style.
When it happens
Trigger: Creating `new Setter()` and adding it to a style without ever setting `.Property`; AXAML `<Setter Value="..."/>` missing the `Property` attribute; object initializer that omits Property.
Common situations: AXAML Setter missing the Property attribute; data-binding the Property and it not being resolved; copy-paste Setter template and forgetting the property name.
Related errors
- Need more than one query to OR.
- Need more than one selector to OR.
- ControlTemplate styles cannot contain multiple template sele
- Child styles must have a nesting selector.
- Name may not be empty
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/947d5471c841a1ba.
Report an issue: GitHub.