dotnet/wpf · error · ArgumentException
SR.StylePropertyInStyleNotAllowed
Error message
SR.StylePropertyInStyleNotAllowed
What it means
A Setter that targets the container (no TargetName set) may not set the StyleProperty itself, since that would recursively redefine the style of the object being styled. WPF throws this ArgumentException during Seal() to prevent an infinite/self-referential style definition.
Solutions
- Remove the Setter targeting the Style property, or move it to a Trigger with a TargetName pointing at a named element inside a template.
- Use a different mechanism such as Style.BasedOn to derive a style instead of setting Style from within a style.
- If restyling is needed at runtime, do it in code (element.Style = newStyle) outside the style/trigger system.
Example fix
<!-- before -->
<Style TargetType="Button">
<Setter Property="Style" Value="{StaticResource OtherStyle}"/>
</Style>
<!-- after -->
<Style TargetType="Button" BasedOn="{StaticResource OtherStyle}"/> Defensive patterns
Strategy: validation
Validate before calling
bool IsLegal(Setter s) => s.Property != FrameworkElement.StyleProperty || !string.IsNullOrEmpty(s.TargetName);
Prevention
- Never set Property="Style" in a container-targeted Setter
- Use BasedOn to compose styles instead of setting Style from within a style
When it happens
Trigger: A Setter with Property="Style" and no TargetName inside a Style or Trigger, e.g. `<Setter Property="Style" .../>` directly in a `<Style>` whose setters apply to the styled element itself.
Common situations: Copy-pasted setter blocks from a ControlTemplate trigger into a Style; attempts to restyle an element from within its own style; refactoring triggers where TargetName was dropped.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.Format(SR.NullPropertyIllegal, "Setter.Property")
- 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/090d33ff51cf191f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs:98
{
// 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)
{
if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )
{
throw new ArgumentException(SR.Format(SR.SetterValueOfMarkupExtensionNotSupported,
value.GetType().Name));
}
}
View on GitHub (pinned to 81131a70a4)