dotnet/wpf · error · ArgumentException
SR.Format(SR.TemplateTargetTypeMismatch…
Error message
SR.Format(SR.TemplateTargetTypeMismatch, "ContentPresenter", templatedParent.GetType().Name)
What it means
DataTemplate.ValidateTemplatedParent throws ArgumentException when a DataTemplate's templated parent is not a ContentPresenter. DataTemplates may only be applied through a ContentPresenter host; other element types must use ControlTemplate or ItemsPanelTemplate.
Solutions
- Use the DataTemplate only via ContentPresenter (e.g. ContentTemplate / ItemTemplate properties)
- If templating a Control, declare a ControlTemplate instead
- Check the host element: typeof(templatedParent) must be ContentPresenter before applying
Example fix
// before
button.Template = (DataTemplate)FindResource("itemTemplate"); // wrong kind
// after
button.Template = (ControlTemplate)FindResource("buttonTemplate");
// or: contentPresenter.ContentTemplate = (DataTemplate)FindResource("itemTemplate"); Defensive patterns
Strategy: validation
Validate before calling
if (templatedParent is not ContentPresenter) throw new InvalidOperationException("DataTemplate requires a ContentPresenter templated parent"); Type guard
bool CanApplyDataTemplate(object parent) => parent is ContentPresenter;
Try / catch
try { ValidateTemplatedParent(templatedParent); } catch (ArgumentException) { /* use ControlTemplate or a ContentPresenter host */ } Prevention
- Assign DataTemplates only to ContentTemplate/ItemTemplate/DataTemplate-type properties
- Use ControlTemplate for the Control.Template property
- Verify host element type before applying templates programmatically
When it happens
Trigger: Calling ApplyTemplate/finding a template host where templatedParent is e.g. a Control (not ContentPresenter); assigning a DataTemplate as a ControlTemplate.
Common situations: Confusing ControlTemplate and DataTemplate in XAML; setting a DataTemplate where a ControlTemplate is expected (e.g. Template property of a Control); custom framework code invoking template validation with the wrong parent.
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
- ' ' is not a Visual or Visual3D.
- 0x80070057
- Animation_AnimationTimelineTypeMismatch
- Property data must be a non-reference variant compatible…
- Property is not a valid instance of PrintTicket.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/cc91faddcba59f95.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DataTemplate.cs:192
//
// Protected Methods
//
//-------------------------------------------------------------------
/// <summary>
/// Validate against the following rules
/// 1. Must have a non-null feTemplatedParent
/// 2. A DataTemplate must be applied to a ContentPresenter
/// </summary>
protected override void ValidateTemplatedParent(FrameworkElement templatedParent)
{
// Must have a non-null feTemplatedParent
ArgumentNullException.ThrowIfNull(templatedParent);
// A DataTemplate must be applied to a ContentPresenter
if (!(templatedParent is ContentPresenter))
{
throw new ArgumentException(SR.Format(SR.TemplateTargetTypeMismatch, "ContentPresenter", templatedParent.GetType().Name));
}
}
#endregion Protected Methods
#region Data
private object _dataType;
private TriggerCollection _triggers;
#endregion Data
}
}
View on GitHub (pinned to 81131a70a4)