{"record":{"id":"090d33ff51cf191f","repo":"dotnet/wpf","slug":"sr-stylepropertyinstylenotallowed","errorCode":null,"errorMessage":"SR.StylePropertyInStyleNotAllowed","messagePattern":"SR\\.StylePropertyInStyleNotAllowed","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs","lineNumber":98,"sourceCode":"        {\n\n            // Do the validation that can't be done until we know all of the property\n            // values.\n\n            DependencyProperty dp = Property;\n            object value = ValueInternal;\n\n            if (dp == null)\n            {\n                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, \"Setter.Property\"));\n            }\n\n            if( String.IsNullOrEmpty(TargetName))\n            {\n                // Setter on container is not allowed to affect the StyleProperty.\n                if (dp == FrameworkElement.StyleProperty)\n                {\n                    throw new ArgumentException(SR.StylePropertyInStyleNotAllowed);\n                }\n            }\n\n            // Value needs to be valid for the DP, or a deferred reference, or one of the supported\n            // markup extensions.\n\n            if (!dp.IsValidValue(value))\n            {\n                // The only markup extensions supported by styles is resources and bindings.\n                if (value is MarkupExtension)\n                {\n                    if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )\n                    {\n                        throw new ArgumentException(SR.Format(SR.SetterValueOfMarkupExtensionNotSupported,\n                                                           value.GetType().Name));\n                    }\n                }\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"<!-- before -->\n<Style TargetType=\"Button\">\n  <Setter Property=\"Style\" Value=\"{StaticResource OtherStyle}\"/>\n</Style>\n<!-- after -->\n<Style TargetType=\"Button\" BasedOn=\"{StaticResource OtherStyle}\"/>","handlingStrategy":"validation","validationCode":"bool IsLegal(Setter s) => s.Property != FrameworkElement.StyleProperty || !string.IsNullOrEmpty(s.TargetName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never set Property=\"Style\" in a container-targeted Setter","Use BasedOn to compose styles instead of setting Style from within a style"],"tags":["wpf","xaml","style","setter"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}