{"record":{"id":"af63bc761870a452","repo":"dotnet/wpf","slug":"sr-format-sr-alreadyhaslogicalchildren-parent-gettype-name","errorCode":null,"errorMessage":"SR.Format(SR.AlreadyHasLogicalChildren, parent.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.AlreadyHasLogicalChildren, parent\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":1111,"sourceCode":"        /// it is actually added to the logical tree because FrameworkContentElement\n        /// has no visual tree.\n        /// </summary>\n        /// <remarks>\n        ///     A prime example of trying to shove a square peg into a round hole.\n        /// </remarks>\n        internal static void AddNodeToLogicalTree( DependencyObject parent, Type type,\n            bool treeNodeIsFE, FrameworkElement treeNodeFE, FrameworkContentElement treeNodeFCE)\n        {\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                {","sourceCodeStart":1093,"sourceCodeEnd":1129,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L1093-L1129","documentation":"AddNodeToLogicalTree, when the parent is a FrameworkContentElement, checks whether that parent already has logical children. Factory-built content is meant to become the sole logical child in this path; if the instantiated parent already enumerates at least one logical child, the framework throws InvalidOperationException(AlreadyHasLogicalChildren) naming the parent type, because it cannot unambiguously attach the factory node.","triggerScenarios":"Instantiating a factory tree where a FrameworkContentElement node (e.g. a Paragraph or another content element) already has children — for example a control whose content was set earlier — and AddNodeToLogicalTree then attempts to attach the factory-built node.","commonSituations":"Reusing a pre-populated control instance as a template root; template content applied over an element whose Content/Children were already populated by prior code; FlowDocument content elements with existing inlines used inside generated templates.","solutions":["Clear the parent's existing content/children before applying the factory-built tree.","Use a fresh, empty parent instance for the template instantiation.","Restructure so the factory node is added through the parent's collection API rather than the single-logical-child path."],"exampleFix":"// before\nparagraph.Inlines.Add(existingRun);\ntemplate.LoadContent(); // factory node attach fails\n// after\nvar freshParagraph = new Paragraph(); // empty parent for factory content","handlingStrategy":"validation","validationCode":"if (parent is FrameworkContentElement fce && fce.LogicalChildren != null && fce.LogicalChildren.MoveNext())\n    throw new InvalidOperationException($\"{fce.GetType().Name} already has logical children; use an empty parent\");","typeGuard":"bool IsEmptyLogicalParent(FrameworkContentElement e) { var it = e.LogicalChildren; return it == null || !it.MoveNext(); }","tryCatchPattern":"try { template.LoadContent(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"logical children\")) { /* clear content or create fresh parent */ }","preventionTips":["Instantiate factory trees into fresh, empty parent instances","Clear Content/Children before re-applying a template","Do not reuse populated FrameworkContentElements as template roots"],"tags":["wpf","template","logical-tree","conflict"],"backgroundTag":"conflicting-config-options","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"}