{"record":{"id":"5b920b72cd14a89a","repo":"dotnet/wpf","slug":"parameter-is-unexpected-type-0-expected-type-is-1","errorCode":null,"errorMessage":"Parameter is unexpected type '{0}'. Expected type is '{1}'.","messagePattern":"Parameter is unexpected type '(.+?)'\\. Expected type is '(.+?)'\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs","lineNumber":91,"sourceCode":"        /// by the parser.\n        ///</summary>\n        ///<param name=\"value\">\n        /// The object to add as a child; it must be a UIElement.\n        ///</param>\n        /// <ExternalAPI/>\n        void IAddChild.AddChild (Object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n            if (IsItemsHost)\n            {\n                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);\n            }\n\n            UIElement uie = value as UIElement;\n\n            if (uie == null)\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));\n            }\n\n            Children.Add(uie);\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// As default Panels do not support text, calling this method has no effect.\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to add as a child.\n        ///</param>\n        void IAddChild.AddText (string text)\n        {\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n\n        #endregion","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs#L73-L109","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Wrap the value in a TextBlock or ContentControl so it is a UIElement","For collections of data, use ItemsControl with ItemsSource/ItemTemplate instead of raw children","Remove the non-UIElement child from the panel markup","In code, cast or construct a UIElement before calling AddChild"],"exampleFix":"// before\n<StackPanel>\n  Hello World <!-- ArgumentException -->\n</StackPanel>\n// after\n<StackPanel>\n  <TextBlock Text=\"Hello World\"/>\n</StackPanel>","handlingStrategy":"type-guard","validationCode":"if (value is UIElement uie)\n{\n    panel.AddChild(uie);\n}\nelse\n{\n    panel.Children.Add(new TextBlock { Text = value?.ToString() });\n}","typeGuard":"bool isUiElement(object o) => o is UIElement;\nif (value is UIElement uie) panel.AddChild(uie); else /* wrap or reject */;","tryCatchPattern":"try { panel.AddChild(value); }\ncatch (ArgumentException ex) when (ex.ParamName == \"value\") { panel.Children.Add(new TextBlock { Text = value?.ToString() }); }","preventionTips":["Only pass UIElement instances to panel.AddChild","In XAML, never put raw text or primitives directly inside a Panel","Wrap strings in TextBlock, data collections in ItemsControl","Validate element types when generating XAML programmatically"],"tags":["wpf","panel","argument"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}