{"record":{"id":"741a287d708fd600","repo":"dotnet/wpf","slug":"sr-format-sr-triggeronstylenotallowedtohavesource-conditions","errorCode":null,"errorMessage":"SR.Format(SR.TriggerOnStyleNotAllowedToHaveSource, conditions[k].SourceName)","messagePattern":"SR\\.Format\\(SR\\.TriggerOnStyleNotAllowedToHaveSource, conditions\\[k\\]\\.SourceName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":742,"sourceCode":"                    TriggerBase trigger = style._visualTriggers[i];\n\n                    // Set things up to handle Setter values\n                    for (int j = 0; j < trigger.PropertyValues.Count; j++)\n                    {\n                        PropertyValue propertyValue = trigger.PropertyValues[j];\n\n                        // Check for trigger rules that act on container\n                        if (propertyValue.ChildName != StyleHelper.SelfName)\n                        {\n                            throw new InvalidOperationException(SR.StyleTriggersCannotTargetTheTemplate);\n                        }\n\n                        TriggerCondition[] conditions = propertyValue.Conditions;\n                        for (int k=0; k<conditions.Length; k++)\n                        {\n                            if( conditions[k].SourceName != StyleHelper.SelfName )\n                            {\n                                throw new InvalidOperationException(SR.Format(SR.TriggerOnStyleNotAllowedToHaveSource, conditions[k].SourceName));\n                            }\n                        }\n\n                        // Track properties on the container that are being driven by\n                        // the Style so that they can be invalidated during style changes\n                        StyleHelper.AddContainerDependent(propertyValue.Property, true /*fromVisualTrigger*/, ref this.ContainerDependents);\n\n                        StyleHelper.UpdateTables(ref propertyValue, ref ChildRecordFromChildIndex,\n                            ref TriggerSourceRecordFromChildIndex, ref ResourceDependents, ref _dataTriggerRecordFromBinding,\n                            null /*_childIndexFromChildID*/, ref _hasInstanceValues);\n                    }\n\n                    // Set things up to handle TriggerActions\n                    if( trigger.HasEnterActions || trigger.HasExitActions )\n                    {\n                        if( trigger is Trigger )\n                        {\n                            StyleHelper.AddPropertyTriggerWithAction( trigger, ((Trigger)trigger).Property, ref this.PropertyTriggersWithActions );","sourceCodeStart":724,"sourceCodeEnd":760,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L724-L760","documentation":"Trigger conditions in a Style may only evaluate properties on the styled element itself. If a TriggerCondition has a SourceName other than StyleHelper.SelfName (i.e. it references a named element), ProcessVisualTriggers throws InvalidOperationException naming that SourceName, because styles have no element-name scope.","triggerScenarios":"Adding a trigger whose condition was built with SourceName=\"someElement\" to Style.Triggers and sealing the style; reusing a template trigger's conditions in a style.","commonSituations":"Sharing trigger objects between ControlTemplate triggers (where SourceName is legal) and Style.Triggers; code-generated conditions that carry a source element name; copying XAML triggers from template to style.","solutions":["Set each TriggerCondition.SourceName to StyleHelper.SelfName (or null-equivalent self reference) for style triggers.","Bind the condition to a property of the styled element itself (e.g. IsMouseOver, IsChecked).","Move the trigger into the ControlTemplate if it must observe a named child element."],"exampleFix":"// before\nvar condition = new Condition(\"btnChild\", Button.IsPressedProperty, true); // SourceName set\n// after\nvar condition = new Condition(Button.IsPressedProperty, true); // source = self","handlingStrategy":"validation","validationCode":"// Before adding a Condition to a style trigger:\nif (condition.SourceName != null && condition.SourceName != StyleHelper.SelfName)\n    throw new InvalidOperationException($\"Condition SourceName '{condition.SourceName}' not allowed in a Style.\");","typeGuard":"static bool StyleConditionValid(Condition c) => c.SourceName == null || c.SourceName == StyleHelper.SelfName;","tryCatchPattern":"try { style.Seal(); }\ncatch (InvalidOperationException ex) { log.Error(\"Trigger condition references named element\", ex); }","preventionTips":["Build style-trigger conditions with the single-argument Condition constructor (self source).","Move SourceName-based conditions into ControlTemplate triggers.","Audit reused trigger/condition objects for leftover SourceName values."],"tags":["wpf","style","triggers","sourcename"],"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"}