{"record":{"id":"c76c6499f3680c7f","repo":"dotnet/wpf","slug":"sr-format-sr-eventtriggeronstylenotallowedtohavetarget","errorCode":null,"errorMessage":"SR.Format(SR.EventTriggerOnStyleNotAllowedToHaveTarget, eventTrigger.SourceName)","messagePattern":"SR\\.Format\\(SR\\.EventTriggerOnStyleNotAllowedToHaveTarget, eventTrigger\\.SourceName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":796,"sourceCode":"                            {\n                                Condition dataCondition = multiDataTrigger.Conditions[k];\n\n                                StyleHelper.AddDataTriggerWithAction(trigger, dataCondition.Binding, ref this.DataTriggersWithActions);\n                            }\n                        }\n                        else\n                        {\n                            throw new InvalidOperationException(SR.Format(SR.UnsupportedTriggerInStyle, trigger.GetType().Name));\n                        }\n                    }\n\n                    // Set things up to handle EventTrigger\n                    EventTrigger eventTrigger = trigger as EventTrigger;\n                    if( eventTrigger != null )\n                    {\n                        if( eventTrigger.SourceName != null && eventTrigger.SourceName.Length > 0 )\n                        {\n                            throw new InvalidOperationException(SR.Format(SR.EventTriggerOnStyleNotAllowedToHaveTarget, eventTrigger.SourceName));\n                        }\n\n                        StyleHelper.ProcessEventTrigger(eventTrigger,\n                                                        null /*_childIndexFromChildID*/,\n                                                        ref _triggerActions,\n                                                        ref EventDependents,\n                                                        null /*_templateFactoryRoot*/,\n                                                        null,\n                                                        ref _eventHandlersStore,\n                                                        ref _hasLoadedChangeHandler);\n                    }\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Serves as a hash function for a particular type, suitable for use in\n        ///     hashing algorithms and data structures like a hash table","sourceCodeStart":778,"sourceCodeEnd":814,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L778-L814","documentation":"WPF throws this when an EventTrigger declared inside a Style has a SourceName (TargetName) set. Styles apply to arbitrary instances of the styled type, so a style cannot route event trigger actions to a named element — only templates and FrameworkElement/FrameContent triggers can resolve names. Style.ProcessVisualTriggers rejects it at Seal() time with an InvalidOperationException.","triggerScenarios":"Calling Style.Seal() (explicitly or implicitly when the style is first used) while its Triggers collection contains an EventTrigger whose SourceName property is a non-empty string.","commonSituations":"Copy-pasting an EventTrigger with TargetName=\"x\" from a ControlTemplate into a <Style.Triggers> block; tooling or refactoring that moved triggers between a template and a style; writing <EventTrigger SourceName=\"Part_Grid\" RoutedEvent=\"Mouse.MouseEnter\"> directly in a style resource.","solutions":["Remove the SourceName/TargetName attribute from the EventTrigger in the style and target the styled element itself.","If you need to animate a named child element, move the trigger into a ControlTemplate/DataTemplate instead of a Style.","Use setter-based triggers (Trigger/MultiTrigger) in the style if the behavior can be expressed as property changes."],"exampleFix":"<!-- before -->\n<Style TargetType=\"Button\">\n  <Style.Triggers>\n    <EventTrigger RoutedEvent=\"MouseEnter\" SourceName=\"btn\"/>\n  </Style.Triggers>\n</Style>\n\n<!-- after -->\n<Style TargetType=\"Button\">\n  <Style.Triggers>\n    <EventTrigger RoutedEvent=\"MouseEnter\"/>\n  </Style.Triggers>\n</Style>","handlingStrategy":"validation","validationCode":"foreach (var t in style.Triggers.OfType<EventTrigger>())\n    if (!string.IsNullOrEmpty(t.SourceName))\n        throw new InvalidOperationException($\"EventTrigger in Style cannot set SourceName '{t.SourceName}'; move it to a template.\");","typeGuard":"bool isValidStyleEventTrigger(EventTrigger t) => string.IsNullOrEmpty(t.SourceName);","tryCatchPattern":"try { style.Seal(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"EventTrigger\")) {\n    // strip SourceName from style EventTriggers and re-seal\n}","preventionTips":["Never add SourceName/TargetName to EventTriggers inside Style.Triggers.","Use named-element targeting only within ControlTemplate/DataTemplate triggers.","Keep a lint/XAML-schema rule that flags TargetName under Style.Triggers."],"tags":["wpf","xaml","styles","eventtrigger"],"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"}