dotnet/wpf · error · ArgumentException
SR.Format(SR.NullPropertyIllegal, "Setter.Property")
Error message
SR.Format(SR.NullPropertyIllegal, "Setter.Property")
What it means
A Setter used inside a Style, ControlTemplate, or Trigger must declare which dependency property it sets. WPF throws this ArgumentException during Setter.Seal() because the Property property is null when the style is sealed. Styles are validated (sealed) when applied, so the failure surfaces when the style is used, not when the Setter object is constructed.
Solutions
- Add the Property attribute to every Setter: `<Setter Property="Background" Value="Red"/>`.
- In code, set setter.Property = SomeUIElement.SomeProperty before adding it to a style or applying the style.
- Validate all setters programmatically before applying the style: foreach (SetterBase s in style.Setters) if (s is Setter st && st.Property == null) throw/log.
Example fix
// before
var s = new Setter { Value = Brushes.Red };
style.Setters.Add(s);
// after
var s = new Setter { Property = Control.BackgroundProperty, Value = Brushes.Red };
style.Setters.Add(s); Defensive patterns
Strategy: validation
Validate before calling
foreach (var s in style.Setters.OfType<Setter>())
if (s.Property == null) throw new InvalidOperationException("Setter missing Property"); Type guard
bool HasProperty(Setter s) => s.Property != null;
Try / catch
try { element.Style = style; }
catch (ArgumentException ex) when (ex.Message.Contains("Setter.Property")) { /* log and fix style */ } Prevention
- Always specify Property on every Setter element in XAML
- When building styles in code, set Property in the object initializer
When it happens
Trigger: A Setter element in XAML without a Property attribute, or a Setter created in code via `new Setter { Value = x }` without assigning Property, then the containing Style/Trigger is sealed (applied to an element).
Common situations: XAML typo like `<Setter Value="Red"/>` missing `Property="Background"`; dynamically building styles in code and forgetting to set the Property; data-bound styles built at runtime.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- SR.StylePropertyInStyleNotAllowed
- SR.Format(SR.CannotHavePropertyInStyle…
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.InvalidSetterValue, value, dp.OwnerType…
- SR.Format(SR.NullPropertyIllegal, "EventSetter.Event")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d55db936da13bc4f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs:90
throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name));
}
}
/// <summary>
/// Seals this setter
/// </summary>
internal override void Seal()
{
// Do the validation that can't be done until we know all of the property
// values.
DependencyProperty dp = Property;
object value = ValueInternal;
if (dp == null)
{
throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, "Setter.Property"));
}
if( String.IsNullOrEmpty(TargetName))
{
// Setter on container is not allowed to affect the StyleProperty.
if (dp == FrameworkElement.StyleProperty)
{
throw new ArgumentException(SR.StylePropertyInStyleNotAllowed);
}
}
// Value needs to be valid for the DP, or a deferred reference, or one of the supported
// markup extensions.
if (!dp.IsValidValue(value))
{
// The only markup extensions supported by styles is resources and bindings.
if (value is MarkupExtension)View on GitHub (pinned to 81131a70a4)