{"record":{"id":"7498c7d4677fb108","repo":"dotnet/wpf","slug":"sr-format-sr-setteronstylenotallowedtohavetarget-setter","errorCode":null,"errorMessage":"SR.Format(SR.SetterOnStyleNotAllowedToHaveTarget, setter.TargetName)","messagePattern":"SR\\.Format\\(SR\\.SetterOnStyleNotAllowedToHaveTarget, setter\\.TargetName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":630,"sourceCode":"            {\n                PropertyValues = new FrugalStructList<System.Windows.PropertyValue>(style.Setters.Count);\n            }\n\n            // Add EventSetters to local EventHandlersStore\n            for (int i = 0; i < style.Setters.Count; i++)\n            {\n                SetterBase setterBase = style.Setters[i];\n                Debug.Assert(setterBase != null, \"Setter collection must contain non-null instances of SetterBase\");\n\n                // Setters are folded into the PropertyValues table only for the current style. The\n                // processing of BasedOn Style properties will occur in subsequent call to ProcessSelfStyle\n                Setter setter = setterBase as Setter;\n                if (setter != null)\n                {\n                    // Style Setters are not allowed to have a child target name - since there are no child nodes in a Style.\n                    if( setter.TargetName != null )\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.SetterOnStyleNotAllowedToHaveTarget, setter.TargetName));\n                    }\n\n                    if (style == this)\n                    {\n                        DynamicResourceExtension dynamicResource = setter.ValueInternal as DynamicResourceExtension;\n                        if (dynamicResource == null)\n                        {\n                            UpdatePropertyValueList( setter.Property, PropertyValueType.Set, setter.ValueInternal );\n                        }\n                        else\n                        {\n                            UpdatePropertyValueList( setter.Property, PropertyValueType.Resource, dynamicResource.ResourceKey );\n                        }\n                    }\n                }\n                else\n                {\n","sourceCodeStart":612,"sourceCodeEnd":648,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L612-L648","documentation":"Style Setters apply to the styled element itself; a Setter inside a Style must not specify TargetName, since styles have no child template nodes to target. ProcessSetters throws InvalidOperationException naming the offending TargetName when it is non-null.","triggerScenarios":"A Setter with TargetName=\"...\" placed in Style.Setters (legal only in ControlTemplate/FrameworkTemplate triggers or Template.Setters).","commonSituations":"Copying a setter from a ControlTemplate.Triggers block (which legitimately uses TargetName) back into Style.Triggers/Setters; XAML where <Setter TargetName=\"partName\"> was written inside a Style instead of inside a template trigger.","solutions":["Remove TargetName from the Setter; set the property directly on the styled element.","If you need to target a named child, move the setter into a ControlTemplate's Triggers with the element defined in the template.","Use Trigger.EnterActions/ExitActions with Storyboard.TargetName inside a template if animation targeting is required."],"exampleFix":"<!-- before -->\n<Style TargetType=\"Button\">\n  <Setter TargetName=\"textContent\" Property=\"TextBlock.Foreground\" Value=\"Red\" />\n</Style>\n<!-- after -->\n<ControlTemplate TargetType=\"Button\">\n  <Triggers>\n    <Trigger Property=\"IsMouseOver\" Value=\"True\">\n      <Setter TargetName=\"textContent\" Property=\"TextBlock.Foreground\" Value=\"Red\" />\n    </Trigger>\n  </Triggers>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"foreach (Setter s in style.Setters.OfType<Setter>())\n    if (s.TargetName != null)\n        throw new InvalidOperationException($\"Setter TargetName '{s.TargetName}' not allowed in a Style.\");","typeGuard":"static bool StyleSetterValid(Setter s) => s.TargetName == null;","tryCatchPattern":"try { style.Seal(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"target\")) { log.Error(\"Setter with TargetName in style\", ex); }","preventionTips":["Never use TargetName on Setters inside a Style; only inside ControlTemplate triggers.","To style child parts, define them in a ControlTemplate and target from template triggers.","Review XAML copied from template trigger blocks for stray TargetName attributes."],"tags":["wpf","style","setter","targetname"],"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-21T21:30:21.729Z"}