{"record":{"id":"fa2c636e0638e452","repo":"dotnet/wpf","slug":"sr-format-sr-templatemustbefe-new-object-rootobject-gettype","errorCode":null,"errorMessage":"SR.Format(SR.TemplateMustBeFE, new object[] { rootObject.GetType().FullName })","messagePattern":"SR\\.Format\\(SR\\.TemplateMustBeFE, new object\\[\\] (.+?)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs","lineNumber":1122,"sourceCode":"                }\n\n                InvalidatePropertiesOnTemplate(container, createdObject);\n            }\n        }\n\n        private static DependencyObject WireRootObjectToParent(object createdObject, DependencyObject rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)\n        {\n            rootObject = createdObject as DependencyObject;\n            if (rootObject != null)\n            {\n                // Add the root to the appropriate tree.\n                if (feContainer != null)\n                {\n                    // Put the root object into FE.Templatechild (must be a UIElement).\n                    UIElement rootElement = rootObject as UIElement;\n                    if (rootElement == null)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.TemplateMustBeFE, new object[] { rootObject.GetType().FullName }));\n                    }\n                    feContainer.TemplateChild = rootElement;\n\n                    Debug.Assert(!(rootElement is FrameworkElement) ||\n                        ((FrameworkElement)rootElement).TemplateChildIndex != -1);\n                }\n                // If we have a container that is not a FE, add to the logical tree of the FEF\n                else if (container != null)\n                {\n                    FrameworkElement feResult;\n                    FrameworkContentElement fceResult;\n                    Helper.DowncastToFEorFCE(rootObject, out feResult, out fceResult, true);\n                    FrameworkElementFactory.AddNodeToLogicalTree((FrameworkContentElement)container,\n                        rootObject.GetType(), feResult != null, feResult, fceResult);\n                }\n\n\n                // Set the TemplateNameScope on the root","sourceCodeStart":1104,"sourceCodeEnd":1140,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs#L1104-L1140","documentation":"WireRootObjectToParent requires the root object of a template's content to be a UIElement when the container is a FrameworkElement (so it can be assigned to TemplateChild). If the template's root XAML object is not a UIElement (e.g. a plain ContentElement or non-visual object), InvalidOperationException is thrown naming the root type.","triggerScenarios":"Defining a ControlTemplate/DataTemplate whose top-level element is not a UIElement (e.g. a Run, TextElement, or arbitrary CLR object) while the templated parent is a FrameworkElement.","commonSituations":"Hand-written or tool-mangled XAML where the root tag is wrong; wrapping content in a non-visual element by mistake; misunderstanding that template roots must be visual elements.","solutions":["Make the template's root element a UIElement-derived type (e.g. Grid, Border, StackPanel).","Wrap non-visual content inside a visual container before returning it from the template.","If content should be non-visual, ensure the template is applied in a content (not visual) position, or use a ContentPresenter."],"exampleFix":"<!-- before -->\n<ControlTemplate TargetType=\"Button\"><Run Text=\"hi\"/></ControlTemplate>\n<!-- after -->\n<ControlTemplate TargetType=\"Button\"><TextBlock Text=\"hi\"/></ControlTemplate>","handlingStrategy":"validation","validationCode":"if (!(rootObject is UIElement))\n    throw new InvalidOperationException($\"Template root must be a UIElement, got {rootObject.GetType()}\");","typeGuard":"static bool IsValidTemplateRoot(object o) => o is UIElement;","tryCatchPattern":"try { ApplyTemplate(t); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"UIElement\")) { /* fix template XAML */ }","preventionTips":["Always make template roots visual elements (Panel, Border, Control-derived).","Test templates by instantiating them in a smoke test.","Never put non-visual elements at the top of a ControlTemplate."],"tags":["wpf","template","xaml","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}