{"record":{"id":"76fe1e85746f1374","repo":"dotnet/wpf","slug":"sr-format-sr-visualtreerootisframeworkelement-nameof","errorCode":null,"errorMessage":"SR.Format(SR.VisualTreeRootIsFrameworkElement, nameof(FrameworkElement), templateRoot.Type.Name)","messagePattern":"SR\\.Format\\(SR\\.VisualTreeRootIsFrameworkElement, nameof\\(FrameworkElement\\), templateRoot\\.Type\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs","lineNumber":374,"sourceCode":"\n\n\n        #region NonPublicMethods\n\n\n        //  ===========================================================================\n        //  Validation methods\n        //  ===========================================================================\n\n        // Validate against the following rules\n        // 1. The VisualTree's root must be a FrameworkElement.\n        private void ValidateVisualTree(FrameworkElementFactory templateRoot)\n        {\n            // The VisualTree's root must be a FrameworkElement.\n            if (templateRoot != null &&\n                typeof(FrameworkContentElement).IsAssignableFrom(templateRoot.Type))\n            {\n                throw new ArgumentException(SR.Format(SR.VisualTreeRootIsFrameworkElement, nameof(FrameworkElement), templateRoot.Type.Name));\n            }\n        }\n\n        //  ===========================================================================\n        //  These methods are invoked when a Template is being sealed\n        //  ===========================================================================\n\n        #region Seal\n\n        internal virtual void ProcessTemplateBeforeSeal()\n        {\n        }\n\n\n\n        /// <summary>\n        /// Seal this FrameworkTemplate\n        /// </summary>","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkTemplate.cs#L356-L392","documentation":"ValidateVisualTree throws ArgumentException (SR.VisualTreeRootIsFrameworkElement) when a template's visual tree root (a FrameworkTemplate built from a VisualTree or factory) is a FrameworkContentElement-derived type. Template visual roots must be visual elements (FrameworkElement), not content elements such as Run or Paragraph.","triggerScenarios":"Setting ControlTemplate/FrameworkTemplate.VisualTree to a factory whose Type derives from FrameworkContentElement (e.g. System.Windows.Documents.Run); building a template in code whose root node is a content element.","commonSituations":"Programmatically constructing templates with FrameworkElementFactory rooted at text/document elements; XAML templates whose outermost tag is a FlowDocument content element; refactoring code-generated templates to use Runs for text.","solutions":["Wrap the content element in a visual host such as TextBlock or a Control-derived element so the template root is a FrameworkElement.","Replace the Run root with TextBlock (which hosts Run content) in the template.","For document content, use FlowDocumentScrollViewer or similar FrameworkElement containers as the template root."],"exampleFix":"// before\nvar f = new FrameworkElementFactory(typeof(Run)); // FrameworkContentElement root\ntemplate.VisualTree = f; // throws on validation\n// after\nvar f = new FrameworkElementFactory(typeof(TextBlock));\nf.SetValue(TextBlock.TextProperty, \"Hello\");\ntemplate.VisualTree = f;","handlingStrategy":"validation","validationCode":"if (typeof(FrameworkContentElement).IsAssignableFrom(factory.Type))\n    throw new ArgumentException(\"Template root must be a FrameworkElement, not \" + factory.Type.Name);","typeGuard":"static bool IsVisualRoot(FrameworkElementFactory f) => f?.Type != null && typeof(FrameworkElement).IsAssignableFrom(f.Type);","tryCatchPattern":null,"preventionTips":["Always root templates in FrameworkElement-derived types (Grid, TextBlock, Border, Control).","Host content elements (Run, Paragraph) inside TextBlock or a viewer control."],"tags":["wpf","templates","visual-tree","invalid-argument-value"],"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"}