{"record":{"id":"70e184229b900e32","repo":"dotnet/wpf","slug":"sr-format-sr-cannothavepropertyintemplate-frameworkelement-70e184","errorCode":null,"errorMessage":"SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.NameProperty.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHavePropertyInTemplate, FrameworkElement\\.NameProperty\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":459,"sourceCode":"            // Check if the template has the DefaultStyleKey property set on the container via its visual triggers.\n            // It is an error to specify the DefaultStyleKeyProperty in your own Template.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.DefaultStyleKeyProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.DefaultStyleKeyProperty.Name));\n            }\n\n            // Check if the template has the OverridesDefaultStyle property set on the container via its visual triggers.\n            // It is an error to specify the OverridesDefaultStyleProperty in your own Template.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.OverridesDefaultStyleProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.OverridesDefaultStyleProperty.Name));\n            }\n\n            // Check if the template has the Name property set on the container via its visual triggers.\n            // It is an error to specify the Name in your own Template.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.NameProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.NameProperty.Name));\n            }\n        }\n\n\n\n        //\n        //  All table datastructures read-lock-free/write-lock\n        //  UpdateTables writes the datastructures, locks set by callers\n        //\n        //  This method\n        //  1. Adds a ChildValueLookup entry to the given ChildRecord.\n        //     This is used in value computation.\n        //  2. Optionally adds a ChildPropertyDependent entry to the given\n        //     ContainerRecordFromProperty list. This is used to invalidate\n        //     container dependents.\n        //  3. Optionally adds a ChildPropertyDependent entry to the given\n        //     ResourceDependents list. This is used when invalidating resource\n        //     references","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L441-L477","documentation":"Thrown by ControlTemplate.SealTemplate when the template's visual triggers set the Name property on the templated container. An element's Name participates in the namescope that the template itself defines, so the template cannot assign it to its own container; WPF throws InvalidOperationException at seal time with the property name in the message.","triggerScenarios":"A Setter with Property=\"Name\" (or x:Name-based setter) inside ControlTemplate.Triggers targeting the container. Detected by StyleHelper.IsSetOnContainer during SealTemplate.","commonSituations":"Generated XAML that serializes element names into trigger setters; copy-paste of property setters including Name from one template to another; code-built templates (FrameworkElementFactory / Trigger setters built programmatically) that add a Name setter.","solutions":["Remove the Name setter from the template triggers.","Use x:Name on the template's named parts and reference them via TemplateBinding/FindName instead.","Pass identity through attached properties or Tag rather than Name.","If building templates in code, skip Setter entries for Name, Style, DefaultStyleKey, and OverridesDefaultStyle on the container."],"exampleFix":"// before (code-built trigger)\nvar trigger = new Trigger { Property = UIElement.IsEnabledProperty, Value = false };\ntrigger.Setters.Add(new Setter(FrameworkElement.NameProperty, \"root\"));\n\n// after\nvar trigger = new Trigger { Property = UIElement.IsEnabledProperty, Value = false };\ntrigger.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Gray));\n// name the part in XAML instead: <Border x:Name=\"root\" .../>","handlingStrategy":"validation","validationCode":"bool templateSetsName(ControlTemplate t) =>\n    t.Triggers.Cast<TriggerBase>()\n     .SelectMany(tr => tr is MultiTrigger mt ? mt.Setters : tr.Setters.Cast<SetterBase>())\n     .OfType<Setter>().Any(s => s.Property == FrameworkElement.NameProperty);","typeGuard":null,"tryCatchPattern":"try { element.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Name\"))\n{\n    // remove the Name setter; use x:Name on template parts instead\n}","preventionTips":["Name template parts with x:Name in the visual tree, never via trigger setters","Use TemplatePart attributes and FindName for part access","Whitelist setter properties when generating templates programmatically","Exclude Name, Style, DefaultStyleKey, OverridesDefaultStyle from any trigger-setter codegen"],"tags":["wpf","xaml","controltemplate","namescope"],"backgroundTag":"invalid-state-transition","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"}