{"record":{"id":"e19e5cfc6f9ad231","repo":"dotnet/wpf","slug":"sr-templatetargettypemismatch","errorCode":null,"errorMessage":"SR.TemplateTargetTypeMismatch","messagePattern":"SR\\.TemplateTargetTypeMismatch","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs","lineNumber":67,"sourceCode":"\n        #region PublicMethods\n\n        /// <summary>\n        ///     Validate against the following rules\n        ///     1. One cannot use a ControlTemplate to template a FrameworkContentElement\n        ///     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","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs#L49-L85","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"<!-- before -->\n<ControlTemplate TargetType=\"Button\">...</ControlTemplate> <!-- applied to CheckBox -->\n<!-- after -->\n<ControlTemplate TargetType=\"CheckBox\">...</ControlTemplate>","handlingStrategy":"validation","validationCode":"if (template.TargetType != null && !template.TargetType.IsInstanceOfType(control)) throw new InvalidOperationException(\"Template TargetType does not match control\");","typeGuard":"bool IsTemplateCompatible(ControlTemplate t, Control c) => t.TargetType == null || t.TargetType.IsInstanceOfType(c);","tryCatchPattern":null,"preventionTips":["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"],"tags":["wpf","controltemplate","type-mismatch","xaml"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}