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

  1. Remove the duplicate <Setter> from the style so each property appears exactly once; keep the one whose Value you intend.
  2. 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.
  3. 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

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


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