dotnet/wpf · error · ArgumentException
SR.Collection_BadType
Error message
SR.Collection_BadType
What it means
UIElementCollection.Cast throws ArgumentException when the supplied value is non-null but not a UIElement (the `as UIElement` cast fails). The message names the collection, the value's runtime type, and the expected UIElement type via SR.Collection_BadType.
Solutions
- Only add UIElement-derived instances to the collection.
- Wrap non-visual data in a suitable container (TextBlock, ContentPresenter, ContentControl).
- Type-check with `is UIElement` before calling Add/Insert on the IList path.
Example fix
// before
panel.Children.Add("Hello");
// after
panel.Children.Add(new TextBlock { Text = "Hello" }); Defensive patterns
Strategy: type-guard
Validate before calling
if (value is UIElement uiElement) panel.Children.Add(uiElement);
Type guard
static bool IsUIElement(object o) => o is UIElement;
Try / catch
try { panel.Children.Add(value); }
catch (ArgumentException ex) { /* value was not a UIElement; wrap or convert it */ } Prevention
- Only add UIElement-derived objects to panel Children.
- Wrap raw data in TextBlock/ContentPresenter before adding.
- Guard IList.Add paths with `is UIElement` checks.
When it happens
Trigger: panel.Children.Add(someNonVisualObject), Insert with a business object or string, or IList.Add through a non-generic reference with a wrong-typed item.
Common situations: Mistakenly adding view-model objects or strings to a panel's Children; refactoring that changed what a helper method returns; generic UI-building code that assumes everything added is a control.
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.BadFixedTextPosition
- SR.Collection_NoNull
- SR.Format(SR.Animation_AnimationTimelineTypeMismatch…
- SR.Format(SR.Animation_AnimationTimelineTypeMismatch…
- SR.Format(SR.Collection_BadDestArray, this.GetType().Name)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/96fa33f4b328a5ec.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs:391
/// <summary>
/// Method that forwards to VisualCollection.Move
/// </summary>
/// <param name="visual"></param>
/// <param name="destination"></param>
internal void MoveVisualChild(Visual visual, Visual destination)
{
_visualChildren.Move(visual, destination);
}
private UIElement Cast(object value)
{
if (value == null)
throw new System.ArgumentException(SR.Format(SR.Collection_NoNull, "UIElementCollection"));
UIElement element = value as UIElement;
if (element == null)
throw new System.ArgumentException(SR.Format(SR.Collection_BadType, "UIElementCollection", value.GetType().Name, "UIElement"));
return element;
}
#region IList Members
/// <summary>
/// Adds an element to the UIElementCollection
/// </summary>
int IList.Add(object value)
{
return Add(Cast(value));
}
/// <summary>
/// Determines whether an element is in the UIElementCollection.
/// </summary>
bool IList.Contains(object value)View on GitHub (pinned to 81131a70a4)