dotnet/wpf · error · ArgumentException
SR.TemplateTargetTypeMismatch
Error message
SR.TemplateTargetTypeMismatch
What it means
ControlTemplate.ValidateTemplatedParent throws this ArgumentException when a ControlTemplate is applied to a control whose type does not match the template's TargetType. WPF requires the template's declared target type to be an instance of (compatible with) the control being templated. This guards against applying, say, a Button template to a CheckBox.
Solutions
- Set the ControlTemplate's TargetType to the actual control type being templated (or a base type of it).
- If the template is meant to be shared, define it with a shared base type like Control as TargetType and use TemplateBinding only on base-class properties.
- Fix the Style Setter or implicit style key so the template is applied only to the intended control type.
Example fix
<!-- before --> <ControlTemplate TargetType="Button">...</ControlTemplate> <!-- applied to CheckBox --> <!-- after --> <ControlTemplate TargetType="CheckBox">...</ControlTemplate>
Defensive patterns
Strategy: validation
Validate before calling
if (template.TargetType != null && !template.TargetType.IsInstanceOfType(control)) throw new InvalidOperationException("Template TargetType does not match control"); Type guard
bool IsTemplateCompatible(ControlTemplate t, Control c) => t.TargetType == null || t.TargetType.IsInstanceOfType(c);
Prevention
- Always set TargetType on ControlTemplate and keep it in sync with the styled control type
- Use x:Type markup extension with the exact control type
- Avoid copy-pasting templates between control types without retargeting
When it happens
Trigger: Applying a ControlTemplate with TargetType=Button to a non-Button control; assigning the template via Style or directly to a control of an incompatible type before ValidateTemplatedParent runs.
Common situations: Copy-pasting a template between controls without updating TargetType; implicit style keys mismatched with the control type; refactoring a control's base class while the template TargetType still names the old type.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.TriggersSupportsEventTriggersOnly
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e19e5cfc6f9ad231.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs:67
#region PublicMethods
/// <summary>
/// Validate against the following rules
/// 1. One cannot use a ControlTemplate to template a FrameworkContentElement
/// 2. One cannot use a ControlTemplate to template a FrameworkElement other than a Control
/// 3. One cannot use a ControlTemplate to template a Control that isn't associated with it
/// </summary>
protected override void ValidateTemplatedParent(FrameworkElement templatedParent)
{
// Must have a non-null feTemplatedParent
ArgumentNullException.ThrowIfNull(templatedParent);
// The target type of a ControlTemplate must match the
// type of the Control that it is being applied to
if (_targetType != null && !_targetType.IsInstanceOfType(templatedParent))
{
throw new ArgumentException(SR.Format(SR.TemplateTargetTypeMismatch, _targetType.Name, templatedParent.GetType().Name));
}
// One cannot use a ControlTemplate to template a Control that isn't associated with it
if (templatedParent.TemplateInternal != this)
{
throw new ArgumentException(SR.MustNotTemplateUnassociatedControl);
}
}
#endregion PublicMethods
#region PublicProperties
/// <summary>
/// TargetType for this ControlTemplate
/// </summary>
[Ambient]
[DefaultValue(null)]
public Type TargetTypeView on GitHub (pinned to 81131a70a4)