dotnet/wpf · error · InvalidOperationException
SR.StyleForWrongType
Error message
SR.StyleForWrongType
What it means
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.
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
Example fix
<!-- before --> <Style TargetType="ContentPresenter"> ... </Style> <!-- after --> <Style TargetType="GroupItem"> ... </Style>
Defensive patterns
Strategy: validation
Validate before calling
if (style != null && style.TargetType != null && !style.TargetType.IsInstanceOfType(groupItem)) throw new InvalidOperationException("Style TargetType does not match container"); Type guard
bool StyleFits(Style s, object o) => s?.TargetType == null || s.TargetType.IsInstanceOfType(o);
Try / catch
try { groupItem.Style = style; } catch (InvalidOperationException ex) when (ex.Message.Contains("TargetType") || ex.Message.Contains("style")) { /* fix TargetType or use matching style */ } Prevention
- Keep Style TargetType aligned with the actual container type
- Avoid reusing styles across different container types
- Prefer explicit keyed styles for custom containers
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- ArgumentOutOfRangeException
- (no message) NotSupportedException
- SR.CyclicStyleReferenceDetected
- SR.CyclicThemeStyleReferenceDetected
- SR.Format(SR.CannotChangeAfterSealed, "trigger")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/346cd5b155fdefe9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GroupItem.cs:183
// apply the container style
Style style = groupStyle.ContainerStyle;
// no ContainerStyle set, try ContainerStyleSelector
if (style == null)
{
if (groupStyle.ContainerStyleSelector != null)
{
style = groupStyle.ContainerStyleSelector.SelectStyle(item, this);
}
}
// apply the style, if found
if (style != null)
{
// verify style is appropriate before applying it
if (!style.TargetType.IsInstanceOfType(this))
throw new InvalidOperationException(SR.Format(SR.StyleForWrongType, style.TargetType.Name, this.GetType().Name));
this.Style = style;
this.WriteInternalFlag2(InternalFlags2.IsStyleSetFromGenerator, true);
}
// forward the header template information
if (ContentIsItem || !HasNonDefaultValue(ContentProperty))
{
this.Content = item;
ContentIsItem = true;
}
if (!HasNonDefaultValue(ContentTemplateProperty))
this.ContentTemplate = groupStyle.HeaderTemplate;
if (!HasNonDefaultValue(ContentTemplateSelectorProperty))
this.ContentTemplateSelector = groupStyle.HeaderTemplateSelector;
if (!HasNonDefaultValue(ContentStringFormatProperty))
this.ContentStringFormat = groupStyle.HeaderStringFormat;
View on GitHub (pinned to 81131a70a4)