dotnet/wpf · error · ArgumentException
SR.MustNotTemplateUnassociatedControl
Error message
SR.MustNotTemplateUnassociatedControl
What it means
ControlTemplate.ValidateTemplatedParent throws this ArgumentException when the control being templated does not have this exact template registered as its TemplateInternal. A ControlTemplate can only be applied to the control instance(s) it was created for via the template system, not re-used ad hoc on unassociated controls.
Solutions
- Apply the template via a Style setter or as a shared resource so each control gets its own template instance through the normal resolution path.
- Never assign controlA.Template to controlB directly; instead re-resolve the template resource for the second control.
- If templating in code, create a new ControlTemplate instance for each control.
Example fix
// before
otherControl.Template = button1.Template;
// after
otherControl.Template = (ControlTemplate)FindResource("MyButtonTemplate"); Defensive patterns
Strategy: type-guard
Validate before calling
if (control.TemplateInternal != null && control.TemplateInternal != template) { /* template not associated */ } Type guard
bool IsAssociated(ControlTemplate t, Control c) => ReferenceEquals(c.TemplateInternal, t);
Try / catch
try { other.Template = someTemplate; } catch (ArgumentException) { other.Template = (ControlTemplate)FindResource("tpl"); } Prevention
- Never share template instances directly between controls in code
- Apply templates via Style setters or resource lookup
When it happens
Trigger: Manually assigning one control's ControlTemplate instance to a different control's Template/TemplateInternal; sharing a single template instance across controls in code instead of using a Style or the template resource system.
Common situations: Code-behind that copies control.Template from one element to another; reusing a template instance built for one control; framework-internal invalidation passing the wrong templated parent.
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
- ArgumentNullException(argName)
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.CannotHavePropertyInTemplate…
- SR.Format(SR.CannotHavePropertyInTemplate…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/066053f12a802ba3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs:73
/// 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 TargetType
{
get { return _targetType; }
set
{
ValidateTargetType(value, "value");
CheckSealed();View on GitHub (pinned to 81131a70a4)