AvaloniaUI/Avalonia · error · InvalidOperationException
Duplicate setter encountered for property '{valueEntry.Prope
Error message
Duplicate setter encountered for property '{valueEntry.Property}' in '{Source}'. What it means
A StyleInstance holds at most one setter per AvaloniaProperty. When the framework materializes setter instances from a Style's Setters collection, StyleInstance.Add throws InvalidOperationException if Contains(property) is already true, i.e. a setter for that exact property was already added. This catches ambiguous style definitions early instead of silently letting the last setter win.
Source
Thrown at src/Avalonia.Base/Styling/StyleInstance.cs:52
FrameType type)
: base(GetPriority(activator), type)
{
_activator = activator;
Source = style;
}
public bool HasActivator => _activator is object;
public IStyle Source { get; }
bool IStyleInstance.IsActive => _isActive;
public void Add(ISetterInstance instance)
{
if (instance is IValueEntry valueEntry)
{
if (Contains(valueEntry.Property))
throw new InvalidOperationException(
$"Duplicate setter encountered for property '{valueEntry.Property}' in '{Source}'.");
Add(valueEntry);
}
else
(_setters ??= new()).Add(instance);
}
public void Add(IList<IAnimation> animations)
{
if (_animations is null)
_animations = new List<IAnimation>(animations);
else
_animations.AddRange(animations);
}
public void ApplyAnimations(AvaloniaObject control)
{
if (_animations is not null && control is Animatable animatable)View on GitHub (pinned to 11c5427268)
Solutions
- Remove the duplicate <Setter> from the style so each property appears exactly once; keep the one whose Value you intend.
- If you are overriding an inherited value, split into a separate Style with a more specific selector rather than duplicating the setter within the same Style.
- If building setters programmatically, check style.Setters for an existing Setter with the same Property before adding, or use a dictionary keyed by property during construction.
Example fix
<!-- before --> <Style Selector="Button.foo"> <Setter Property="Background" Value="Red"/> <Setter Property="Background" Value="Blue"/> </Style> <!-- after --> <Style Selector="Button.foo"> <Setter Property="Background" Value="Blue"/> </Style>
Defensive patterns
Strategy: validation
Validate before calling
// De-duplicate setters by property before adding to a Style.
var seen = new HashSet<AvaloniaProperty>(style.Setters.OfType<Setter>().Select(s => s.Property));
foreach (var s in settersToAdd)
{
if (seen.Add(s.Property)) style.Setters.Add(s);
} Type guard
// Detect duplicate setter properties in a style definition.
static bool HasDuplicateSetters(Style style)
{
var props = style.Setters.OfType<Setter>().Select(s => s.Property);
return props.GroupBy(p => p).Any(g => g.Count() > 1);
} Prevention
- Review XAML styles for repeated Property attributes after copy-paste.
- When generating styles programmatically, key setters by AvaloniaProperty in a dictionary.
- Run a build-time or unit-test check that scans loaded styles for duplicate setters.
- Use a more-specific selector to override a value rather than re-declaring it in the same Style.
When it happens
Trigger: Defining two <Setter> elements with the same Property in a single <Style> in XAML, or calling style.Setters.Add twice with two Setter instances whose Property is the same AvaloniaProperty. Also triggered by generated/templated style builders that append setters without de-duplicating by property.
Common situations: Copy-paste errors in XAML styles where a property is set twice; style builders that merge base and override setters without dedup; overrides that re-declare an inherited setter inside the same Style rather than using a separate more-specific selector; animation/transition setters colliding with value setters.
Related errors
- Cannot add {child.GetType()} to a style.
- The Style already has a parent.
- Could not convert '{s}' to {typeof(T)}.
- Unexpected end of expression.
- Invalid attached property name.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/eba2203379f1a471.
Report an issue: GitHub.