{"record":{"id":"066053f12a802ba3","repo":"dotnet/wpf","slug":"sr-mustnottemplateunassociatedcontrol","errorCode":null,"errorMessage":"SR.MustNotTemplateUnassociatedControl","messagePattern":"SR\\.MustNotTemplateUnassociatedControl","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs","lineNumber":73,"sourceCode":"        ///     2. One cannot use a ControlTemplate to template a FrameworkElement other than a Control\n        ///     3. One cannot use a ControlTemplate to template a Control that isn't associated with it\n        /// </summary>\n        protected override void ValidateTemplatedParent(FrameworkElement templatedParent)\n        {\n            // Must have a non-null feTemplatedParent\n            ArgumentNullException.ThrowIfNull(templatedParent);\n\n            // The target type of a ControlTemplate must match the \n            // type of the Control that it is being applied to\n            if (_targetType != null && !_targetType.IsInstanceOfType(templatedParent))\n            {\n                throw new ArgumentException(SR.Format(SR.TemplateTargetTypeMismatch, _targetType.Name, templatedParent.GetType().Name));\n            }\n\n            // One cannot use a ControlTemplate to template a Control that isn't associated with it\n            if (templatedParent.TemplateInternal != this)\n            {\n                throw new ArgumentException(SR.MustNotTemplateUnassociatedControl);\n            }\n        }\n        #endregion PublicMethods\n\n        #region PublicProperties\n\n        /// <summary>\n        ///     TargetType for this ControlTemplate\n        /// </summary>\n        [Ambient]\n        [DefaultValue(null)]\n        public Type TargetType\n        {\n            get {  return _targetType; }\n            set\n            {\n                ValidateTargetType(value, \"value\");\n                CheckSealed();","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs#L55-L91","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\notherControl.Template = button1.Template;\n// after\notherControl.Template = (ControlTemplate)FindResource(\"MyButtonTemplate\");","handlingStrategy":"type-guard","validationCode":"if (control.TemplateInternal != null && control.TemplateInternal != template) { /* template not associated */ }","typeGuard":"bool IsAssociated(ControlTemplate t, Control c) => ReferenceEquals(c.TemplateInternal, t);","tryCatchPattern":"try { other.Template = someTemplate; } catch (ArgumentException) { other.Template = (ControlTemplate)FindResource(\"tpl\"); }","preventionTips":["Never share template instances directly between controls in code","Apply templates via Style setters or resource lookup"],"tags":["wpf","controltemplate","templating"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}