dotnet/wpf · error · ArgumentException

SR.InvalidControlTemplateTargetType

Error message

SR.InvalidControlTemplateTargetType

What it means

ControlTemplate.ValidateTargetType throws this ArgumentException when the TargetType is not Control, Page, PageFunctionBase, or a subclass of one of them. Only these types may be templated by a ControlTemplate.

Solutions

  1. Change TargetType to a Control-derived type (Button, CheckBox, Control base, etc.).
  2. To template a Window, derive from Window/Control appropriately or use the supported templating mechanism for that element type.
  3. For non-control visuals, use a DataTemplate instead of a ControlTemplate.

Example fix

<!-- before -->
<ControlTemplate TargetType="Window">...</ControlTemplate>
<!-- after -->
<ControlTemplate TargetType="Control">...</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

bool ok = typeof(Control).IsAssignableFrom(t) || typeof(Page).IsAssignableFrom(t) || typeof(PageFunctionBase).IsAssignableFrom(t);

Type guard

bool IsValidTemplateTarget(Type t) => t != null && (typeof(Control).IsAssignableFrom(t) || typeof(Page).IsAssignableFrom(t) || typeof(PageFunctionBase).IsAssignableFrom(t));

Prevention

When it happens

Trigger: Setting TargetType to a non-control type such as a Window, FrameworkElement subclass that isn't a Control, or any arbitrary class.

Common situations: Trying to template a Window (which uses a DataTemplate/other mechanism, not ControlTemplate); targeting a Panel or ContentElement that is not a Control; typos picking the wrong type.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        #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
        {
            get 
            {  
                if (TargetType != null)
                {
                    return TargetType; 

View on GitHub (pinned to 81131a70a4)