{"record":{"id":"f1b69f91a00d2c7d","repo":"dotnet/wpf","slug":"sr-frameworkelementfactorycannotaddtext","errorCode":null,"errorMessage":"SR.FrameworkElementFactoryCannotAddText","messagePattern":"SR\\.FrameworkElementFactoryCannotAddText","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":77,"sourceCode":"        }\n\n\n        /// <summary>\n        ///     Type of object that the factory will produce\n        /// </summary>\n        public Type Type\n        {\n            get { return _type; }\n            set\n            {\n                if (_sealed)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\"));\n                }\n\n                if (_text != null)\n                {\n                    throw new InvalidOperationException(SR.FrameworkElementFactoryCannotAddText);\n                }\n\n                if ( value != null ) // We allow null up until Seal\n                {\n                    // If non-null, must be derived from one of the supported types\n                    if (!typeof(FrameworkElement).IsAssignableFrom(value) &&\n                        !typeof(FrameworkContentElement).IsAssignableFrom(value) &&\n                        !typeof(Visual3D).IsAssignableFrom(value))\n                    {\n                        throw new ArgumentException(SR.Format(SR.MustBeFrameworkOr3DDerived, value.Name));\n                    }\n                }\n\n                // It is possible that _type is null when a FEF is created for text content within a tag\n                _type = value;\n\n                // If this is a KnownType in the BamlSchemaContext, then there is a faster way to create\n                // an instance of that type than using Activator.CreateInstance.  So in that case","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L59-L95","documentation":"FrameworkElementFactory.Type setter throws this InvalidOperationException when the factory already holds literal text content (_text != null) and you try to assign a Type. A FrameworkElementFactory can describe either text content or an element type, never both. WPF throws early so the invalid template tree is never built.","triggerScenarios":"Calling factory.Text = \"some text\" (or the internal text path) and then assigning factory.Type = typeof(...) on the same FrameworkElementFactory instance.","commonSituations":"Building a DataTemplate/ControlTemplate programmatically where one code path sets Text and a later refactoring or shared helper also sets Type on the same factory; merging template-building code from two branches.","solutions":["Decide on one content mode: either set Text or set Type, not both, on the same factory instance.","If both are needed, create a separate FrameworkElementFactory for the text and attach it via AppendChild to the typed parent factory.","Check factory.Text is null before assigning Type (guard with an if or clear Text first by setting it to null before Seal)."],"exampleFix":"// before\nvar fef = new FrameworkElementFactory();\nfef.Text = \"hello\";\nfef.Type = typeof(TextBlock); // throws\n\n// after\nvar fef = new FrameworkElementFactory(typeof(TextBlock));\nfef.SetValue(TextBlock.TextProperty, \"hello\");","handlingStrategy":"validation","validationCode":"if (factory.Text != null)\n    throw new InvalidOperationException(\"Cannot set Type: factory already has Text content.\");\nfactory.Type = typeof(TextBlock);","typeGuard":"bool CanSetType(FrameworkElementFactory f) => f.Text is null;","tryCatchPattern":null,"preventionTips":["Pick text OR type per factory at construction time and enforce it in your builder API.","Use constructor overload new FrameworkElementFactory(typeof(T)) to make the mode explicit.","Never expose raw FrameworkElementFactory mutation to callers; wrap in a builder that tracks mode."],"tags":["wpf","xaml","frameworkelementfactory","invalid-state"],"backgroundTag":"mutually-exclusive-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"}