dotnet/wpf · error · InvalidOperationException
SR.Format(SR.StyleForWrongType, style.TargetType.Name…
Error message
SR.Format(SR.StyleForWrongType, style.TargetType.Name, container.GetType().Name)
What it means
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.
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.
Example fix
// before
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListViewItem"><Setter Property="HorizontalContentAlignment" Value="Stretch"/></Style>
</ListBox.ItemContainerStyle>
</ListBox>
// after
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem"><Setter Property="HorizontalContentAlignment" Value="Stretch"/></Style>
</ListBox.ItemContainerStyle>
</ListBox> Defensive patterns
Strategy: type-guard
Validate before calling
bool IsContainerStyleCompatible(Style style, FrameworkElement container) =>
style == null || style.TargetType == null || style.TargetType.IsInstanceOfType(container); Type guard
bool IsStyleFor<TContainer>(Style style) where TContainer : class => style?.TargetType?.IsAssignableFrom(typeof(TContainer)) == true;
Try / catch
try { itemsControl.ItemContainerStyle = style; } catch (InvalidOperationException ex) when (ex.Message.Contains("TargetType")) { /* assign a style with corrected TargetType */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- SR.Format(SR.StyleTargetTypeMismatchWithElement…
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- A panel with IsItemsHost="true" is not nested in an…
- Cannot explicitly modify Children collection of Panel used…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d8a1b1fc8643b9af.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs:3477
// Control's ItemContainerStyle has first stab
Style style = ItemContainerStyle;
// no ItemContainerStyle set, try ItemContainerStyleSelector
if (style == null)
{
if (ItemContainerStyleSelector != null)
{
style = ItemContainerStyleSelector.SelectStyle(item, container);
}
}
// apply the style, if found
if (style != null)
{
// verify style is appropriate before applying it
if (!style.TargetType.IsInstanceOfType(container))
throw new InvalidOperationException(SR.Format(SR.StyleForWrongType, style.TargetType.Name, container.GetType().Name));
foContainer.Style = style;
foContainer.IsStyleSetFromGenerator = true;
}
else if (foContainer.IsStyleSetFromGenerator)
{
// if Style was formerly set from ItemContainerStyle, clear it
foContainer.IsStyleSetFromGenerator = false;
container.ClearValue(FrameworkElement.StyleProperty);
}
}
private void RemoveItemContainerStyle(DependencyObject container)
{
FrameworkObject foContainer = new FrameworkObject(container);
if (foContainer.IsStyleSetFromGenerator)
{View on GitHub (pinned to 81131a70a4)