{"record":{"id":"d464dc59ba0ccb08","repo":"dotnet/wpf","slug":"sr-templatecontentsettwice","errorCode":null,"errorMessage":"SR.TemplateContentSetTwice","messagePattern":"SR\\.TemplateContentSetTwice","errorType":"exception","errorClass":"XamlParseException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs","lineNumber":199,"sourceCode":"        {\n            get\n            {\n                return _templateHolder;\n            }\n            set\n            {\n                CheckSealed();\n\n                if (!_hasXamlNodeContent)\n                {\n                    value.OwnerTemplate = this;\n                    value.ParseXaml();\n                    _templateHolder = value;\n                    _hasXamlNodeContent = true;\n                }\n                else\n                {\n                    throw new System.Windows.Markup.XamlParseException(SR.TemplateContentSetTwice);\n                }\n            }\n        }\n\n        /// <summary>\n        ///     The collection of resources that can be\n        ///     consumed by the container and its sub-tree.\n        /// </summary>\n        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]\n        [Ambient]\n        public ResourceDictionary Resources\n        {\n            get\n            {\n                // Verify Context Access\n                VerifyAccess();\n\n                if ( _resources == null )","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs#L181-L217","documentation":"FrameworkTemplate.Content (or the Xaml-set template content) throws XamlParseException (SR.TemplateContentSetTwice) when template content is assigned more than once. A template may hold either a XAML node stream or a VisualTree/Factory definition, and assigning content after it was already set would silently discard the first definition.","triggerScenarios":"Assigning FrameworkTemplate.Content twice; loading XAML that defines template content and then programmatically setting Content on the same template instance; reusing a deserialized ControlTemplate/FrameworkTemplate object with new content.","commonSituations":"Reusing a single ControlTemplate instance for multiple controls while also re-setting its content; template hot-reload code that reassigns Content; building templates in code after parsing one from XAML.","solutions":["Create a new ControlTemplate/FrameworkTemplate instance for each distinct content assignment.","Set template content exactly once, at construction/XAML load time.","If content must vary, swap whole templates on the control (control.Template = newTemplate) instead of mutating one template."],"exampleFix":"// before\nControlTemplate ct = (ControlTemplate)FindResource(\"tpl\");\nct.Content = buttonFactory; // throws if XAML content already set\n// after\nControlTemplate ct = new ControlTemplate(typeof(Button));\nct.Content = buttonFactory;","handlingStrategy":"validation","validationCode":"if (templateHasContentAlready) // track your own assignment flag\n    template = new ControlTemplate(targetType);\ntemplate.Content = newContent;","typeGuard":null,"tryCatchPattern":"try { template.Content = value; }\ncatch (XamlParseException) { template = new ControlTemplate(targetType) { Content = value }; }","preventionTips":["Assign template content exactly once.","Instantiate a new template per distinct content definition.","Swap whole templates on controls rather than editing loaded ones."],"tags":["wpf","templates","xaml","invalid-state-transition"],"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"}