{"record":{"id":"cc91faddcba59f95","repo":"dotnet/wpf","slug":"sr-format-sr-templatetargettypemismatch-contentpresenter","errorCode":null,"errorMessage":"SR.Format(SR.TemplateTargetTypeMismatch, \"ContentPresenter\", templatedParent.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TemplateTargetTypeMismatch, \"ContentPresenter\", templatedParent\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DataTemplate.cs","lineNumber":192,"sourceCode":"        //\n        //  Protected Methods\n        //\n        //-------------------------------------------------------------------\n\n        /// <summary>\n        ///     Validate against the following rules\n        ///     1. Must have a non-null feTemplatedParent\n        ///     2. A DataTemplate must be applied to a ContentPresenter\n        /// </summary>\n        protected override void ValidateTemplatedParent(FrameworkElement templatedParent)\n        {\n            // Must have a non-null feTemplatedParent\n            ArgumentNullException.ThrowIfNull(templatedParent);\n\n            // A DataTemplate must be applied to a ContentPresenter\n            if (!(templatedParent is ContentPresenter))\n            {\n                throw new ArgumentException(SR.Format(SR.TemplateTargetTypeMismatch, \"ContentPresenter\", templatedParent.GetType().Name));\n            }\n        }\n\n        #endregion Protected Methods\n\n        #region Data\n\n        private object                  _dataType;\n        private TriggerCollection       _triggers;\n\n        #endregion Data\n    }\n}\n\n","sourceCodeStart":174,"sourceCodeEnd":207,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DataTemplate.cs#L174-L207","documentation":"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.","triggerScenarios":"Calling ApplyTemplate/finding a template host where templatedParent is e.g. a Control (not ContentPresenter); assigning a DataTemplate as a ControlTemplate.","commonSituations":"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.","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"],"exampleFix":"// before\nbutton.Template = (DataTemplate)FindResource(\"itemTemplate\"); // wrong kind\n// after\nbutton.Template = (ControlTemplate)FindResource(\"buttonTemplate\");\n// or: contentPresenter.ContentTemplate = (DataTemplate)FindResource(\"itemTemplate\");","handlingStrategy":"validation","validationCode":"if (templatedParent is not ContentPresenter) throw new InvalidOperationException(\"DataTemplate requires a ContentPresenter templated parent\");","typeGuard":"bool CanApplyDataTemplate(object parent) => parent is ContentPresenter;","tryCatchPattern":"try { ValidateTemplatedParent(templatedParent); } catch (ArgumentException) { /* use ControlTemplate or a ContentPresenter host */ }","preventionTips":["Assign DataTemplates only to ContentTemplate/ItemTemplate/DataTemplate-type properties","Use ControlTemplate for the Control.Template property","Verify host element type before applying templates programmatically"],"tags":["wpf","datatemplate","contentpresenter","type-mismatch"],"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"}