{"record":{"id":"f44fb77458d67833","repo":"dotnet/wpf","slug":"sr-invalidcontroltemplatetargettype","errorCode":null,"errorMessage":"SR.InvalidControlTemplateTargetType","messagePattern":"SR\\.InvalidControlTemplateTargetType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs","lineNumber":138,"sourceCode":"\n        #endregion PublicProperties\n\n        #region NonPublicMethods\n\n        // Validate against two rules\n        //  1. targetType must not null\n        //  2. targetType must be a Control or a subclass of it\n        private void ValidateTargetType(Type targetType, string argName)\n        {\n            if (targetType == null)\n            {\n                throw new ArgumentNullException(argName);\n            }\n            if (!typeof(Control).IsAssignableFrom(targetType) &&\n                !typeof(Page).IsAssignableFrom(targetType) &&\n                !typeof(PageFunctionBase).IsAssignableFrom(targetType))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidControlTemplateTargetType, targetType.Name));\n            }\n        }\n        \n        #endregion NonPublicMethods\n\n        #region NonPublicProperties\n        \n        //\n        //  TargetType for ControlTemplate. This is override is \n        //  so FrameworkTemplate can see this property.\n        //\n        internal override Type TargetTypeInternal\n        {\n            get \n            {  \n                if (TargetType != null)\n                {\n                    return TargetType; ","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs#L120-L156","documentation":"ControlTemplate.ValidateTargetType throws this ArgumentException when the TargetType is not Control, Page, PageFunctionBase, or a subclass of one of them. Only these types may be templated by a ControlTemplate.","triggerScenarios":"Setting TargetType to a non-control type such as a Window, FrameworkElement subclass that isn't a Control, or any arbitrary class.","commonSituations":"Trying to template a Window (which uses a DataTemplate/other mechanism, not ControlTemplate); targeting a Panel or ContentElement that is not a Control; typos picking the wrong type.","solutions":["Change TargetType to a Control-derived type (Button, CheckBox, Control base, etc.).","To template a Window, derive from Window/Control appropriately or use the supported templating mechanism for that element type.","For non-control visuals, use a DataTemplate instead of a ControlTemplate."],"exampleFix":"<!-- before -->\n<ControlTemplate TargetType=\"Window\">...</ControlTemplate>\n<!-- after -->\n<ControlTemplate TargetType=\"Control\">...</ControlTemplate>","handlingStrategy":"validation","validationCode":"bool ok = typeof(Control).IsAssignableFrom(t) || typeof(Page).IsAssignableFrom(t) || typeof(PageFunctionBase).IsAssignableFrom(t);","typeGuard":"bool IsValidTemplateTarget(Type t) => t != null && (typeof(Control).IsAssignableFrom(t) || typeof(Page).IsAssignableFrom(t) || typeof(PageFunctionBase).IsAssignableFrom(t));","tryCatchPattern":null,"preventionTips":["Only target Control/Page/PageFunctionBase-derived types","Use DataTemplate for non-control content","Remember Window cannot be the TargetType of a ControlTemplate"],"tags":["wpf","controltemplate","invalid-target-type"],"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-21T21:30:21.729Z"}