dotnet/wpf · error

Parameter is unexpected type

Error message

Parameter is unexpected type '{0}'. Expected type is '{1}'.

What it means

Panel.IAddChild.AddChild accepts only UIElement values; passing any other object type throws ArgumentException naming the actual and expected types. This is the XAML parser's add-child path, so it fires when non-UIElement content is declared inside a Panel element.

Solutions

  1. Wrap the value in a TextBlock or ContentControl so it is a UIElement
  2. For collections of data, use ItemsControl with ItemsSource/ItemTemplate instead of raw children
  3. Remove the non-UIElement child from the panel markup
  4. In code, cast or construct a UIElement before calling AddChild

Example fix

// before
<StackPanel>
  Hello World <!-- ArgumentException -->
</StackPanel>
// after
<StackPanel>
  <TextBlock Text="Hello World"/>
</StackPanel>
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is UIElement uie)
{
    panel.AddChild(uie);
}
else
{
    panel.Children.Add(new TextBlock { Text = value?.ToString() });
}

Type guard

bool isUiElement(object o) => o is UIElement;
if (value is UIElement uie) panel.AddChild(uie); else /* wrap or reject */;

Try / catch

try { panel.AddChild(value); }
catch (ArgumentException ex) when (ex.ParamName == "value") { panel.Children.Add(new TextBlock { Text = value?.ToString() }); }

Prevention

When it happens

Trigger: Declaring a string, number, or arbitrary non-UIElement object as a direct child of <StackPanel>/<Grid>/<Canvas> in XAML, or calling panel.AddChild("text") in code.

Common situations: XAML like <StackPanel>Hello</StackPanel> instead of <TextBlock Text="Hello"/>; putting primitives or business objects directly in a panel instead of binding them through an ItemsControl; accidental markup nesting mistakes.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5b920b72cd14a89a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs:91

        /// by the parser.
        ///</summary>
        ///<param name="value">
        /// The object to add as a child; it must be a UIElement.
        ///</param>
        /// <ExternalAPI/>
        void IAddChild.AddChild (Object value)
        {
            ArgumentNullException.ThrowIfNull(value);
            if (IsItemsHost)
            {
                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);
            }

            UIElement uie = value as UIElement;

            if (uie == null)
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));
            }

            Children.Add(uie);
        }

        ///<summary>
        /// This method is called by the parser when text appears under the tag in markup.
        /// As default Panels do not support text, calling this method has no effect.
        ///</summary>
        ///<param name="text">
        /// Text to add as a child.
        ///</param>
        void IAddChild.AddText (string text)
        {
            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
        }

        #endregion

View on GitHub (pinned to 81131a70a4)