{"record":{"id":"3bdc11d92528e513","repo":"dotnet/wpf","slug":"sr-format-sr-cannothookupfceroot-type-name","errorCode":null,"errorMessage":"SR.Format(SR.CannotHookupFCERoot, type.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHookupFCERoot, type\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":1119,"sourceCode":"        {\n            // If the logical parent already has children, then we can't add\n            // a logical subtree from the style, since there would be a conflict.\n            // Throw an exception in this case.\n            FrameworkContentElement logicalParent = parent as FrameworkContentElement;\n            if (logicalParent != null)\n            {\n                IEnumerator childEnumerator = logicalParent.LogicalChildren;\n                if (childEnumerator != null && childEnumerator.MoveNext())\n                {\n                    throw new InvalidOperationException(SR.Format(SR.AlreadyHasLogicalChildren,\n                                                          parent.GetType().Name));\n                }\n            }\n\n            IAddChild  addChildParent = parent as IAddChild;\n            if (addChildParent == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHookupFCERoot,\n                                                          type.Name));\n            }\n            else\n            {\n                if (treeNodeFE != null)\n                {\n                    addChildParent.AddChild(treeNodeFE);\n                }\n                else\n                {\n                    addChildParent.AddChild(treeNodeFCE);\n                }\n            }\n        }\n\n        // This method is also used by XamlStyleSerializer to decide whether\n        // or not to emit the Name attribute for a VisualTree node.\n        internal bool IsChildNameValid(string childName)","sourceCodeStart":1101,"sourceCodeEnd":1137,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L1101-L1137","documentation":"AddNodeToLogicalTree requires a way to attach the factory-built node to its parent. When the parent is not a FrameworkElement, not a FrameworkContentElement with valid handling, and does not implement IAddChild, there is no available hookup mechanism for the FCE root, so the framework throws InvalidOperationException(CannotHookupFCERoot) naming the type. Essentially the parent type lacks any supported child-attachment contract.","triggerScenarios":"Using a type as a factory parent (root or interior node) that is neither FrameworkElement, nor handled FrameworkContentElement, nor IAddChild — e.g. an arbitrary DependencyObject or plain object used as the root of a FrameworkElementFactory tree — at template instantiation time.","commonSituations":"Setting DataTemplate.VisualTree to a factory whose root type cannot host children; using non-visual or non-content types as template roots; typos where the wrong type was passed to the factory constructor.","solutions":["Use a supported root type: FrameworkElement (Panel, ContentControl, etc.) or a type implementing IAddChild.","If the root must be a FrameworkContentElement, ensure it supports the content model expected by this path.","Check the type passed to new FrameworkElementFactory(type) — it likely should be a visual/content control."],"exampleFix":"// before\nvar root = new FrameworkElementFactory(typeof(DispatchersObject)); // not FE/FCE/IAddChild\n// after\nvar root = new FrameworkElementFactory(typeof(Grid));","handlingStrategy":"type-guard","validationCode":"if (!typeof(FrameworkElement).IsAssignableFrom(rootType) &&\n    !typeof(IAddChild).IsAssignableFrom(rootType))\n    throw new InvalidOperationException($\"{rootType.Name} cannot be a factory root\");","typeGuard":"bool IsValidFactoryRoot(Type t) => typeof(FrameworkElement).IsAssignableFrom(t) || typeof(IAddChild).IsAssignableFrom(t);","tryCatchPattern":"try { template.LoadContent(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"hookup\") || ex.Message.Contains(\"root\")) { /* use a supported root type */ }","preventionTips":["Choose FrameworkElement-derived roots for DataTemplate.VisualTree","Validate the type passed to the FrameworkElementFactory constructor","Avoid arbitrary DependencyObjects as template roots"],"tags":["wpf","template","type-mismatch","unsupported-operation"],"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"}