dotnet/wpf · error · InvalidOperationException
SR.InvalidItemContainer
Error message
SR.InvalidItemContainer
What it means
StatusBar.GetContainerForItemOverride verifies that a generated or returned item container is either a StatusBarItem or a Separator. If PrepareContainerForItemOverride/GetContainerForItemOverride receives anything else, it throws InvalidOperationException naming the StatusBar type and the two valid container types. This guards StatusBar's invariant that every direct child container is one of the two supported types.
Solutions
- Return a StatusBarItem (or Separator) from GetContainerForItemOverride, or call base.GetContainerForItemOverride()
- If custom content is needed, wrap it inside StatusBarItem.Content rather than replacing the container type
- Remove any override or style that changes the ItemContainer for the StatusBar
Example fix
// before
protected override DependencyObject GetContainerForItemOverride()
=> new ListBoxItem(); // wrong container -> throws
// after
protected override DependencyObject GetContainerForItemOverride()
=> new StatusBarItem(); Defensive patterns
Strategy: try-catch
Validate before calling
protected override DependencyObject GetContainerForItemOverride()
{
var c = base.GetContainerForItemOverride();
if (c is not StatusBarItem and not Separator)
throw new InvalidOperationException("StatusBar containers must be StatusBarItem or Separator.");
return c;
} Try / catch
try
{
statusBar.Items.Add(container);
}
catch (InvalidOperationException ex)
{
// wrap in StatusBarItem before adding
} Prevention
- Always return StatusBarItem/Separator from GetContainerForItemOverride overrides
- Don't copy ListBox container code into StatusBar subclasses
- Use StatusBarItem.Content for custom visuals
When it happens
Trigger: Overriding GetContainerForItemOverride/PrepareContainerForItemOverride in a StatusBar subclass and returning a wrong container type (e.g. ContentPresenter, ListBoxItem); a custom ItemsPanel or style forcing a different container; reusing a ListBox-derived container factory for StatusBar.
Common situations: Customizing StatusBar item presentation by subclassing; copying container-generation code from ListBox into StatusBar; templates that implicitly produce a different container.
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
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- Microsoft.Windows.Controls.SR.Ribbon_ContextualTabHeadersSou…
- SR.AdornedElementNotFound
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/eaae74b047db1ef1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/StatusBar.cs:123
protected override DependencyObject GetContainerForItemOverride()
{
object currentItem = _currentItem;
_currentItem = null;
if (UsesItemContainerTemplate)
{
DataTemplate itemContainerTemplate = ItemContainerTemplateSelector.SelectTemplate(currentItem, this);
if (itemContainerTemplate != null)
{
object itemContainer = itemContainerTemplate.LoadContent();
if (itemContainer is StatusBarItem || itemContainer is Separator)
{
return itemContainer as DependencyObject;
}
else
{
throw new InvalidOperationException(SR.Format(SR.InvalidItemContainer, this.GetType().Name, nameof(StatusBarItem), nameof(Separator), itemContainer));
}
}
}
return new StatusBarItem();
}
/// <summary>
/// Prepare the element to display the item. This may involve
/// applying styles, setting bindings, etc.
/// </summary>
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
Separator separator = element as Separator;
if (separator != null)
{
bool hasModifiers;View on GitHub (pinned to 81131a70a4)