{"record":{"id":"c7171f556277ffc3","repo":"dotnet/wpf","slug":"storyboard-targetnamenotallowedinstyle","errorCode":"Storyboard_TargetNameNotAllowedInStyle","errorMessage":"SR.Format(SR.Storyboard_TargetNameNotAllowedInStyle, nameString)","messagePattern":"SR\\.Format\\(SR\\.Storyboard_TargetNameNotAllowedInStyle, nameString\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs","lineNumber":397,"sourceCode":"        Int64 layer                        /* Remains the same through all the recursive calls */)\n    {\n        Timeline currentTimeline = currentClock.Timeline;\n\n        DependencyObject targetObject = parentObject;\n        string currentObjectName = parentObjectName;\n        PropertyPath currentPropertyPath = parentPropertyPath;\n\n        // If we have target object/property information, use it instead of the\n        //  parent's information.\n        string nameString = (string)currentTimeline.GetValue(TargetNameProperty);\n        if( nameString != null )\n        {\n            if( nameScope is Style )\n            {\n                // We are inside a Style - we don't let people target anything.\n                //  They're only allowed to modify the Styled object, which is\n                //  already the implicit target.\n                throw new InvalidOperationException(SR.Format(SR.Storyboard_TargetNameNotAllowedInStyle, nameString));\n            }\n            currentObjectName = nameString;\n        }\n\n        // The TargetProperty trumps the TargetName property.\n        DependencyObject localTargetObject = (DependencyObject) currentTimeline.GetValue(TargetProperty);\n        if( localTargetObject != null )\n        {\n            targetObject = localTargetObject;\n            currentObjectName = null;\n        }\n\n        PropertyPath propertyPath = (PropertyPath)currentTimeline.GetValue(TargetPropertyProperty);\n        if( propertyPath != null )\n        {\n            currentPropertyPath = propertyPath;\n        }\n","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs#L379-L415","documentation":"During ClockTreeWalkRecursive (invoked from Storyboard.Begin), a timeline has a TargetName while the current namescope is a Style. Styles forbid targeting other objects — animations inside a Style may only affect the styled (implicit target) object — so any TargetName is rejected with an InvalidOperationException.","triggerScenarios":"Defining a Storyboard inside a <Style> (Setters/Triggers/EnterActions/ExitActions) where a child timeline sets Storyboard.TargetName=\"someElement\", then beginning the storyboard via trigger actions.","commonSituations":"Trying to animate another control from a Style trigger; porting a storyboard from Window resources into a Style without removing TargetName; misunderstanding that Style animations implicitly target the styled element.","solutions":["Remove TargetName (and Target) from the timeline in the Style; the animation implicitly targets the styled element — specify only Storyboard.TargetProperty.","Move the storyboard to Window/UserControl resources and trigger it via EventTrigger with SourceName if cross-element targeting is required.","Use a ControlTemplate trigger with TargetName if targeting named template parts (allowed in templates, not styles)."],"exampleFix":"<!-- before: throws -->\n<Style TargetType=\"Button\">\n  <Style.Triggers>\n    <Trigger Property=\"IsMouseOver\" Value=\"True\">\n      <Trigger.EnterActions>\n        <Storyboard>\n          <DoubleAnimation Storyboard.TargetName=\"otherPanel\" Storyboard.TargetProperty=\"Opacity\" To=\"1\"/>\n        </Storyboard>\n      </Trigger.EnterActions>\n    </Trigger>\n  </Style.Triggers>\n</Style>\n\n<!-- after: implicit target only -->\n<DoubleAnimation Storyboard.TargetProperty=\"Opacity\" To=\"1\"/>","handlingStrategy":"validation","validationCode":"// In a Style, fail fast if a timeline declares TargetName\nif (inStyle && timeline.GetValue(Storyboard.TargetNameProperty) != null)\n    throw new InvalidOperationException(\"Storyboard.TargetName is not allowed inside a Style\");","typeGuard":null,"tryCatchPattern":"try { BeginStoryboard(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Style\")) {\n    // remove TargetName or move the storyboard out of the Style\n}","preventionTips":["Omit TargetName/Target inside Styles — the styled element is the implicit target.","Use ControlTemplate triggers with TargetName for template parts, not Styles.","Keep cross-element storyboards in Window/UserControl resources."],"tags":["wpf","style","storyboard","xaml","namescope"],"backgroundTag":"unsupported-operation","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"}