dotnet/wpf · error · InvalidOperationException

SR.NullTypeIllegal

Error message

SR.NullTypeIllegal

What it means

The private Seal throws InvalidOperationException when the factory has neither a Type nor Text set. A FrameworkElementFactory must know what element to create; sealing with both _type and _text null means no valid factory content was configured. This fires when the factory is being finalized (e.g. when a template using it is being sealed).

Solutions

  1. Construct the factory with a target type: new FrameworkElementFactory(typeof(TargetElement)).
  2. If textual content is intended, set the factory's Text property.
  3. Ensure the configuration path actually runs before assigning the factory to a template.

Example fix

// before
var factory = new FrameworkElementFactory();
template.VisualTree = factory;
// after
var factory = new FrameworkElementFactory(typeof(Border));
template.VisualTree = factory;
Defensive patterns

Strategy: validation

Validate before calling

if (factory.GetType() == null && string.IsNullOrEmpty(factory.Text))
    throw new InvalidOperationException("Factory needs a Type or Text before sealing.");

Type guard

static bool IsConfigured(FrameworkElementFactory f) => f != null && (f.Type != null || !string.IsNullOrEmpty(f.Text));

Try / catch

try { template.Seal(); }
catch (InvalidOperationException) { template.VisualTree = new FrameworkElementFactory(typeof(DefaultContent)); }

Prevention

When it happens

Trigger: Sealing a template whose VisualTree is an empty FrameworkElementFactory created with the parameterless constructor and never assigned a type, text, or children context.

Common situations: new FrameworkElementFactory() left unconfigured but attached to a DataTemplate/ControlTemplate; conditional configuration code paths that skip setting the type.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ca6dbea3cd9f2274. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs:544

        internal void Seal(FrameworkTemplate ownerTemplate)
        {
            if (_sealed)
            {
                return;
            }

            // Store owner Template
            _frameworkTemplate = ownerTemplate;

            // Perform actual Seal operation
            Seal();
        }

        private void Seal()
        {
            if (_type == null && _text == null)
            {
                throw new InvalidOperationException(SR.NullTypeIllegal);
            }

            if (_firstChild != null)
            {
                // This factory has children, it must implement IAddChild so that these
                // children can be added to the logical tree
                if (!typeof(IAddChild).IsAssignableFrom(_type))
                {
                    throw new InvalidOperationException(SR.Format(SR.TypeMustImplementIAddChild, _type.Name));
                }
            }

            ApplyAutoAliasRules();

            if ((_childName != null) && (_childName != String.Empty))
            {
                // ChildName provided
                if (!IsChildNameValid(_childName))

View on GitHub (pinned to 81131a70a4)