dotnet/wpf · error · ArgumentException

SR.StylePropertyInStyleNotAllowed

Error message

SR.StylePropertyInStyleNotAllowed

What it means

TriggerBase.ProcessParametersContainer throws ArgumentException with SR.StylePropertyInStyleNotAllowed when a trigger targets the Style property itself (FrameworkElement.StyleProperty). A style is not allowed to affect its own Style property — it would create self-reference semantics WPF forbids.

Solutions

  1. Remove the Setter/trigger that targets the Style property from the style
  2. Use a separate parent style or a DataTrigger/StyleSelector to change styles
  3. Target a different property (or use template triggers for visual changes)

Example fix

// before
<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="Style" Value="{StaticResource HoverStyle}"/>
</Trigger>
// after
<!-- change an inner visual property instead -->
<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="Background" Value="LightBlue"/>
</Trigger>
Defensive patterns

Strategy: validation

Validate before calling

if (setter.Property == FrameworkElement.StyleProperty)
    throw new ArgumentException("A style cannot target its own Style property.");

Type guard

static bool IsAllowedSetterProperty(DependencyProperty dp) => dp != FrameworkElement.StyleProperty;

Try / catch

try { BuildTriggers(); }
catch (ArgumentException ex) when (ex.Message.Contains("Style")) { /* remove Style-targeting setter */ }

Prevention

When it happens

Trigger: Defining a Setter or trigger SetXxx with Property="Style" inside a Style or template, e.g. <Setter Property="Style" .../> within <Style.Triggers> or a PropertyTrigger referencing StyleProperty.

Common situations: XAML authoring mistake: trying to swap the control's style from within its own style's triggers; copy-paste of setters across style scopes.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7a6f7476580b66a5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerBase.cs:177

                {
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "trigger"));
                }

                _executeExitActionsOnApply = value;
            }
        }

*/
        /// <summary>
        ///     Parameter validation work common to the SetXXXX methods that deal
        /// with the container node of the Style/Template.
        /// </summary>
        internal void ProcessParametersContainer(DependencyProperty dp)
        {
            // Not allowed to use Style to affect the StyleProperty.
            if (dp == FrameworkElement.StyleProperty)
            {
                throw new ArgumentException(SR.StylePropertyInStyleNotAllowed);
            }
        }

        /// <summary>
        ///     Parameter validation work common to the SetXXXX methods that deal
        /// with visual tree child nodes.
        /// </summary>
        internal string ProcessParametersVisualTreeChild(DependencyProperty dp, string target)
        {
            ArgumentNullException.ThrowIfNull(target);

            if (target.Length == 0)
            {
                throw new ArgumentException(SR.ChildNameMustBeNonEmpty);
            }

            return String.Intern(target);
        }

View on GitHub (pinned to 81131a70a4)