{"record":{"id":"eba2203379f1a471","repo":"AvaloniaUI/Avalonia","slug":"duplicate-setter-encountered-for-property-valuee","errorCode":null,"errorMessage":"Duplicate setter encountered for property '{valueEntry.Property}' in '{Source}'.","messagePattern":"Duplicate setter encountered for property '(.+?)' in '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Styling/StyleInstance.cs","lineNumber":52,"sourceCode":"            FrameType type)\n            : base(GetPriority(activator), type)\n        {\n            _activator = activator;\n            Source = style;\n        }\n\n        public bool HasActivator => _activator is object;\n\n        public IStyle Source { get; }\n\n        bool IStyleInstance.IsActive => _isActive;\n        \n        public void Add(ISetterInstance instance)\n        {\n            if (instance is IValueEntry valueEntry)\n            {\n                if (Contains(valueEntry.Property))\n                    throw new InvalidOperationException(\n                        $\"Duplicate setter encountered for property '{valueEntry.Property}' in '{Source}'.\");\n                Add(valueEntry);\n            }\n            else\n                (_setters ??= new()).Add(instance);\n        }\n\n        public void Add(IList<IAnimation> animations)\n        {\n            if (_animations is null)\n                _animations = new List<IAnimation>(animations);\n            else\n                _animations.AddRange(animations);\n        }\n\n        public void ApplyAnimations(AvaloniaObject control)\n        {\n            if (_animations is not null && control is Animatable animatable)","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Styling/StyleInstance.cs#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"<!-- before -->\n<Style Selector=\"Button.foo\">\n  <Setter Property=\"Background\" Value=\"Red\"/>\n  <Setter Property=\"Background\" Value=\"Blue\"/>\n</Style>\n\n<!-- after -->\n<Style Selector=\"Button.foo\">\n  <Setter Property=\"Background\" Value=\"Blue\"/>\n</Style>","handlingStrategy":"validation","validationCode":"// De-duplicate setters by property before adding to a Style.\nvar seen = new HashSet<AvaloniaProperty>(style.Setters.OfType<Setter>().Select(s => s.Property));\nforeach (var s in settersToAdd)\n{\n    if (seen.Add(s.Property)) style.Setters.Add(s);\n}","typeGuard":"// Detect duplicate setter properties in a style definition.\nstatic bool HasDuplicateSetters(Style style)\n{\n    var props = style.Setters.OfType<Setter>().Select(s => s.Property);\n    return props.GroupBy(p => p).Any(g => g.Count() > 1);\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["avalonia","styling","setters","xaml","duplicate","invalid-operation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}