dotnet/wpf · error · ArgumentNullException

ArgumentNullException(argName)

Error message

ArgumentNullException(argName)

What it means

ControlTemplate.ValidateTargetType throws ArgumentNullException when a null TargetType is supplied to the ControlTemplate constructor or the TargetType setter. WPF requires every ControlTemplate to declare a non-null target type so it can validate the templated control.

Solutions

  1. Pass a concrete Control/Page/PageFunction-derived Type to the ControlTemplate constructor or TargetType setter.
  2. Null-check the type variable before constructing the template.
  3. In XAML always specify TargetType on the ControlTemplate.

Example fix

// before
var tpl = new ControlTemplate(null);
// after
var tpl = new ControlTemplate(typeof(Button));
Defensive patterns

Strategy: validation

Validate before calling

if (targetType == null) throw new ArgumentException("TargetType is required");

Type guard

bool HasTargetType(ControlTemplate t) => t?.TargetType != null;

Try / catch

try { var tpl = new ControlTemplate(type); } catch (ArgumentNullException) { /* supply a default control type */ }

Prevention

When it happens

Trigger: new ControlTemplate(null) or new ControlTemplate(typeof(X), null); assigning TargetType = null in code; XAML omitting TargetType where the parser passes null through code paths that validate.

Common situations: Building templates programmatically and forgetting to pass the type; dynamic template construction where the type variable is null; refactoring that removed the typeof(...) argument.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs:132

                        _triggers.Seal();
                    }
                }
                return _triggers; 
            }
        }

        #endregion PublicProperties

        #region NonPublicMethods

        // Validate against two rules
        //  1. targetType must not null
        //  2. targetType must be a Control or a subclass of it
        private void ValidateTargetType(Type targetType, string argName)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException(argName);
            }
            if (!typeof(Control).IsAssignableFrom(targetType) &&
                !typeof(Page).IsAssignableFrom(targetType) &&
                !typeof(PageFunctionBase).IsAssignableFrom(targetType))
            {
                throw new ArgumentException(SR.Format(SR.InvalidControlTemplateTargetType, targetType.Name));
            }
        }
        
        #endregion NonPublicMethods

        #region NonPublicProperties
        
        //
        //  TargetType for ControlTemplate. This is override is 
        //  so FrameworkTemplate can see this property.
        //
        internal override Type TargetTypeInternal

View on GitHub (pinned to 81131a70a4)