{"record":{"id":"ba1d1a1e320ddd69","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeframeworkor3dderived-value-name","errorCode":null,"errorMessage":"SR.Format(SR.MustBeFrameworkOr3DDerived, value.Name)","messagePattern":"SR\\.Format\\(SR\\.MustBeFrameworkOr3DDerived, value\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":87,"sourceCode":"            {\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\n                // save the delegate for later creation.\n                WpfKnownType knownType = null;\n                if (_type != null)\n                {\n                    knownType = XamlReader.BamlSharedSchemaContext.GetKnownXamlType(_type) as WpfKnownType;\n                }\n                _knownTypeFactory = knownType?.DefaultConstructor;\n            }\n        }\n","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L69-L105","documentation":"The FrameworkElementFactory.Type setter only accepts types derived from FrameworkElement, FrameworkContentElement, or Visual3D. Assigning any other Type throws this ArgumentException naming the offending type. FrameworkElementFactory can only instantiate WPF framework-level elements for template content.","triggerScenarios":"Setting factory.Type to typeof(string), a POCO, a struct, a non-Visual custom class, or any type outside the three supported hierarchies.","commonSituations":"Trying to host arbitrary CLR objects directly in a template instead of binding data; generating templates via reflection where the fetched Type is a model type, not a control; targeting types from assemblies not referencing PresentationFramework.","solutions":["Only assign types deriving from FrameworkElement (e.g. TextBlock, Border), FrameworkContentElement, or Visual3D.","For non-visual data, keep the CLR object as DataContext/Content and use a FrameworkElement-derived presenter inside the template.","Pre-validate the candidate Type before assignment with the same IsAssignableFrom checks WPF uses."],"exampleFix":"// before\nfactory.Type = typeof(MyDataRecord); // throws\n\n// after\nfactory.Type = typeof(ContentPresenter); // FrameworkElement-derived, show data via bindings","handlingStrategy":"validation","validationCode":"static bool IsSupportedElementType(Type t) =>\n    t is not null &&\n    (typeof(FrameworkElement).IsAssignableFrom(t) ||\n     typeof(FrameworkContentElement).IsAssignableFrom(t) ||\n     typeof(Visual3D).IsAssignableFrom(t));\n\nif (!IsSupportedElementType(candidateType))\n    throw new ArgumentException($\"{candidateType.Name} must derive from FrameworkElement, FrameworkContentElement, or Visual3D.\");","typeGuard":"bool IsValidFactoryType(Type? t) => t is not null && (typeof(FrameworkElement).IsAssignableFrom(t) || typeof(FrameworkContentElement).IsAssignableFrom(t) || typeof(Visual3D).IsAssignableFrom(t));","tryCatchPattern":"try { factory.Type = candidateType; }\ncatch (ArgumentException ex) { logger.LogWarning(ex, \"Unsupported factory type {Type}\", candidateType.Name); }","preventionTips":["Only pass typeof(...) of known WPF controls into FrameworkElementFactory.","For generic CLR data, bind via DataContext rather than instantiating the type in a template.","Add a unit test that enumerates your generated template types and asserts hierarchy support."],"tags":["wpf","xaml","frameworkelementfactory","type-validation"],"backgroundTag":"type-mismatch","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"}