{"record":{"id":"d8a1b1fc8643b9af","repo":"dotnet/wpf","slug":"sr-format-sr-styleforwrongtype-style-targettype-name","errorCode":null,"errorMessage":"SR.Format(SR.StyleForWrongType, style.TargetType.Name, container.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.StyleForWrongType, style\\.TargetType\\.Name, container\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs","lineNumber":3477,"sourceCode":"\n            // Control's ItemContainerStyle has first stab\n            Style style = ItemContainerStyle;\n\n            // no ItemContainerStyle set, try ItemContainerStyleSelector\n            if (style == null)\n            {\n                if (ItemContainerStyleSelector != null)\n                {\n                    style = ItemContainerStyleSelector.SelectStyle(item, container);\n                }\n            }\n\n            // apply the style, if found\n            if (style != null)\n            {\n                // verify style is appropriate before applying it\n                if (!style.TargetType.IsInstanceOfType(container))\n                    throw new InvalidOperationException(SR.Format(SR.StyleForWrongType, style.TargetType.Name, container.GetType().Name));\n\n                foContainer.Style = style;\n                foContainer.IsStyleSetFromGenerator = true;\n            }\n            else if (foContainer.IsStyleSetFromGenerator)\n            {\n                // if Style was formerly set from ItemContainerStyle, clear it\n                foContainer.IsStyleSetFromGenerator = false;\n                container.ClearValue(FrameworkElement.StyleProperty);\n            }\n        }\n\n        private void RemoveItemContainerStyle(DependencyObject container)\n        {\n            FrameworkObject foContainer = new FrameworkObject(container);\n\n            if (foContainer.IsStyleSetFromGenerator)\n            {","sourceCodeStart":3459,"sourceCodeEnd":3495,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs#L3459-L3495","documentation":"When the ItemsControl generates a container (e.g. ListBoxItem) it applies the resolved ItemContainerStyle only if style.TargetType is instance-compatible with the container (style.TargetType.IsInstanceOfType(container)). A style whose TargetType does not match the actual container type is rejected with an InvalidOperationException naming both types.","triggerScenarios":"Setting ItemContainerStyle (or a style resolved for the container) with TargetType e.g. ListViewItem on a ListBox (whose containers are ListBoxItem), or a TargetType mismatch after changing the control type; also reusing one container style across different ItemsControl types.","commonSituations":"Copying container styles between ListBox and ListView (ListBoxItem vs ListViewItem); refactoring the control type without updating ItemContainerStyle TargetType; implicit styles keyed to the wrong container type.","solutions":["Change the style's TargetType to the actual container type (e.g. ListBoxItem for ListBox).","Remove TargetType from the style if it is shared across container types and the setters do not require it.","Ensure the ItemContainerStyle is assigned to the matching control type."],"exampleFix":"// before\n<ListBox>\n  <ListBox.ItemContainerStyle>\n    <Style TargetType=\"ListViewItem\"><Setter Property=\"HorizontalContentAlignment\" Value=\"Stretch\"/></Style>\n  </ListBox.ItemContainerStyle>\n</ListBox>\n// after\n<ListBox>\n  <ListBox.ItemContainerStyle>\n    <Style TargetType=\"ListBoxItem\"><Setter Property=\"HorizontalContentAlignment\" Value=\"Stretch\"/></Style>\n  </ListBox.ItemContainerStyle>\n</ListBox>","handlingStrategy":"type-guard","validationCode":"bool IsContainerStyleCompatible(Style style, FrameworkElement container) =>\n    style == null || style.TargetType == null || style.TargetType.IsInstanceOfType(container);","typeGuard":"bool IsStyleFor<TContainer>(Style style) where TContainer : class => style?.TargetType?.IsAssignableFrom(typeof(TContainer)) == true;","tryCatchPattern":"try { itemsControl.ItemContainerStyle = style; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"TargetType\")) { /* assign a style with corrected TargetType */ }","preventionTips":["Match the style TargetType to the container type the control actually generates (ListBoxItem, ListViewItem, TreeViewItem, ComboBoxItem).","Use x:Type with the exact container type; verify when refactoring control types.","Do not share one container style across control families without checking TargetType."],"tags":["wpf","style","targettype","itemscontrol"],"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"}