{"record":{"id":"346cd5b155fdefe9","repo":"dotnet/wpf","slug":"sr-styleforwrongtype","errorCode":null,"errorMessage":"SR.StyleForWrongType","messagePattern":"SR\\.StyleForWrongType","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GroupItem.cs","lineNumber":183,"sourceCode":"\n            // apply the container style\n            Style style = groupStyle.ContainerStyle;\n\n            // no ContainerStyle set, try ContainerStyleSelector\n            if (style == null)\n            {\n                if (groupStyle.ContainerStyleSelector != null)\n                {\n                    style = groupStyle.ContainerStyleSelector.SelectStyle(item, this);\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(this))\n                    throw new InvalidOperationException(SR.Format(SR.StyleForWrongType, style.TargetType.Name, this.GetType().Name));\n\n                this.Style = style;\n                this.WriteInternalFlag2(InternalFlags2.IsStyleSetFromGenerator, true);\n            }\n\n            // forward the header template information\n            if (ContentIsItem || !HasNonDefaultValue(ContentProperty))\n            {\n                this.Content = item;\n                ContentIsItem = true;\n            }\n            if (!HasNonDefaultValue(ContentTemplateProperty))\n                this.ContentTemplate = groupStyle.HeaderTemplate;\n            if (!HasNonDefaultValue(ContentTemplateSelectorProperty))\n                this.ContentTemplateSelector = groupStyle.HeaderTemplateSelector;\n            if (!HasNonDefaultValue(ContentStringFormatProperty))\n                this.ContentStringFormat = groupStyle.HeaderStringFormat;\n","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GroupItem.cs#L165-L201","documentation":"GroupItem.PrepareItemContainer applies the ItemContainerStyle (or generator-provided style) to the GroupItem, but first verifies the style's TargetType is compatible with the GroupItem's actual type. If style.TargetType is not an instance-of type of the container, InvalidOperationException is thrown, since applying an incompatible style would cause property setter failures.","triggerScenarios":"An ItemsControl's GroupStyle or ItemContainerStyle targets a type other than the actual container (e.g. a Style whose TargetType is GroupItem subclass A used where container is GroupItem subclass B, or a style targeting ContentPresenter/other type applied to GroupItem).","commonSituations":"Copy-pasting a Style between container types; changing the container type (e.g. custom GroupItem) without updating Style TargetType; implicit style resolution picking a style typed for a different class.","solutions":["Set the Style's TargetType to GroupItem or a base type of the actual container","Use x:Key'd styles applied explicitly to matching containers","Remove TargetType constraints or use BasedOn correctly when deriving containers"],"exampleFix":"<!-- before -->\n<Style TargetType=\"ContentPresenter\"> ... </Style>\n<!-- after -->\n<Style TargetType=\"GroupItem\"> ... </Style>","handlingStrategy":"validation","validationCode":"if (style != null && style.TargetType != null && !style.TargetType.IsInstanceOfType(groupItem)) throw new InvalidOperationException(\"Style TargetType does not match container\");","typeGuard":"bool StyleFits(Style s, object o) => s?.TargetType == null || s.TargetType.IsInstanceOfType(o);","tryCatchPattern":"try { groupItem.Style = style; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"TargetType\") || ex.Message.Contains(\"style\")) { /* fix TargetType or use matching style */ }","preventionTips":["Keep Style TargetType aligned with the actual container type","Avoid reusing styles across different container types","Prefer explicit keyed styles for custom containers"],"tags":["wpf","styles","grouping","typemismatch"],"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"}