{"record":{"id":"82272e84818cd147","repo":"dotnet/wpf","slug":"sr-frameworkelementfactoryalreadyparented","errorCode":null,"errorMessage":"SR.FrameworkElementFactoryAlreadyParented","messagePattern":"SR\\.FrameworkElementFactoryAlreadyParented","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":167,"sourceCode":"        }\n\n\n        /// <summary>\n        ///     Add a factory child to this factory\n        /// </summary>\n        /// <param name=\"child\">Child to add</param>\n        public void AppendChild(FrameworkElementFactory child)\n        {\n            if (_sealed)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\"));\n            }\n\n            ArgumentNullException.ThrowIfNull(child);\n\n            if (child._parent != null)\n            {\n                throw new ArgumentException(SR.FrameworkElementFactoryAlreadyParented);\n            }\n\n            if (_text != null)\n            {\n                throw new InvalidOperationException(SR.FrameworkElementFactoryCannotAddText);\n            }\n\n            // Build tree of factories\n            if (_firstChild == null)\n            {\n                _firstChild = child;\n                _lastChild = child;\n            }\n            else\n            {\n                _lastChild._nextSibling = child;\n                _lastChild = child;\n            }","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L149-L185","documentation":"FrameworkElementFactory.AppendChild throws this ArgumentException when the child factory already has a parent (_parent != null). Each factory node can belong to at most one tree; re-parenting an existing node elsewhere is rejected to keep the factory graph a strict tree.","triggerScenarios":"Appending the same FrameworkElementFactory instance to two parents, or appending a factory that was already added to another template's tree.","commonSituations":"Reusing a shared child factory (e.g. a common header factory) in multiple templates; copy-paste template builders that forget to new up a child per parent.","solutions":["Create a new FrameworkElementFactory instance for each parent instead of reusing one instance.","Extract a builder method that constructs a fresh child factory per call site.","Check child._parent-equivalent state (or track parents yourself) before appending."],"exampleFix":"// before\nvar shared = new FrameworkElementFactory(typeof(TextBlock));\nparentA.AppendChild(shared);\nparentB.AppendChild(shared); // throws\n\n// after\nparentA.AppendChild(MakeTextChild());\nparentB.AppendChild(MakeTextChild()); // fresh instance per parent\n\nstatic FrameworkElementFactory MakeTextChild() => new(typeof(TextBlock));","handlingStrategy":"validation","validationCode":"// track parents yourself, WPF's _parent is internal\nif (parents.ContainsKey(child))\n    throw new InvalidOperationException(\"Child factory already belongs to another parent; create a new instance.\");\nparent.AppendChild(child);\nparents[child] = parent;","typeGuard":null,"tryCatchPattern":"try { parent.AppendChild(child); }\ncatch (ArgumentException) { parent.AppendChild(CloneFactory(child)); }","preventionTips":["Never share FrameworkElementFactory instances across templates or parents.","Use factory-returning builder methods instead of shared singletons.","Add a debug assertion that each factory is appended exactly once."],"tags":["wpf","xaml","frameworkelementfactory","argument-validation"],"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"}