{"record":{"id":"a1873a98aad9ad65","repo":"dotnet/wpf","slug":"sr-format-sr-templatetargettypemismatch-itemspresenter","errorCode":null,"errorMessage":"SR.Format(SR.TemplateTargetTypeMismatch, \"ItemsPresenter\", templatedParent.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TemplateTargetTypeMismatch, \"ItemsPresenter\", templatedParent\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsPanelTemplate.cs","lineNumber":155,"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 ItemsPanelTemplate 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 ItemsPanelTemplate must be applied to an ItemsPresenter\n            if (!(templatedParent is ItemsPresenter))\n            {\n                throw new ArgumentException(SR.Format(SR.TemplateTargetTypeMismatch, \"ItemsPresenter\", templatedParent.GetType().Name));\n            }\n        }\n\n        #endregion Protected Methods\n    }\n}\n\n\n","sourceCodeStart":137,"sourceCodeEnd":164,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsPanelTemplate.cs#L137-L164","documentation":"ItemsPanelTemplate can only be applied to an ItemsPresenter. ValidateTemplatedParent throws ArgumentException when FindTemplatedParent/ApplyTemplate supplies a templatedParent that is not an ItemsPresenter, because ItemsPanelTemplate is semantically tied to the presenter that hosts generated items.","triggerScenarios":"Applying an ItemsPanelTemplate to a control other than ItemsPresenter, e.g. assigning it to TemplateProperty of a ListBox/ComboBox/ContentPresenter, calling FrameworkElement.ApplyTemplate with a mismatched template, or setting ItemsPanelTemplate as Template in styles/finders.","commonSituations":"Confusing Template= with ItemsPanel= in a style; using ItemsPanelTemplate where ControlTemplate is expected in a custom control's default style; programmatic template application on the wrong element.","solutions":["Apply ItemsPanelTemplate only via ItemsControl.ItemsPanel (or to a real ItemsPresenter).","If you meant to restyle the whole control, use a ControlTemplate instead of ItemsPanelTemplate.","In custom controls, ensure the templated parent at ApplyTemplate time is an ItemsPresenter (place <ItemsPresenter/> in the control template)."],"exampleFix":"<!-- before -->\n<Style TargetType=\"ListBox\">\n  <Setter Property=\"Template\">\n    <Setter.Value>\n      <ItemsPanelTemplate>...</ItemsPanelTemplate>\n    </Setter.Value>\n  </Setter>\n</Style>\n<!-- after -->\n<Style TargetType=\"ListBox\">\n  <Setter Property=\"ItemsPanel\">\n    <Setter.Value>\n      <ItemsPanelTemplate>...</ItemsPanelTemplate>\n    </Setter.Value>\n  </Setter>\n</Style>","handlingStrategy":"type-guard","validationCode":"if (templatedParent is not ItemsPresenter)\n    throw new ArgumentException($\"ItemsPanelTemplate must target an ItemsPresenter, got {templatedParent?.GetType().Name}\");","typeGuard":"static bool CanApplyItemsPanelTemplate(DependencyObject parent) => parent is ItemsPresenter;","tryCatchPattern":"try { element.Template = itemsPanelTemplate; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"ItemsPresenter\")) { /* switch to ControlTemplate for non-presenter targets */ }","preventionTips":["Use ItemsPanelTemplate only via ItemsControl.ItemsPanel, never via Template.","Remember Template= expects ControlTemplate; ItemsPanel= expects ItemsPanelTemplate.","In custom controls, include <ItemsPresenter/> in the ControlTemplate so the panel has a valid target."],"tags":["wpf","templates","itemspresenter","invalid-argument"],"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"}