{"record":{"id":"96fa33f4b328a5ec","repo":"dotnet/wpf","slug":"sr-collection-badtype-uielementcollection","errorCode":null,"errorMessage":"SR.Collection_BadType","messagePattern":"SR\\.Collection_BadType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs","lineNumber":391,"sourceCode":"        /// <summary>\n        /// Method that forwards to VisualCollection.Move\n        /// </summary>\n        /// <param name=\"visual\"></param>\n        /// <param name=\"destination\"></param>\n        internal void MoveVisualChild(Visual visual, Visual destination)\n        {\n            _visualChildren.Move(visual, destination);\n        }\n\t\t\n        private UIElement Cast(object value)\n        {\n            if (value == null)\n                throw new System.ArgumentException(SR.Format(SR.Collection_NoNull, \"UIElementCollection\"));\n\n            UIElement element = value as UIElement;\n\n            if (element == null)\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, \"UIElementCollection\", value.GetType().Name, \"UIElement\"));\n\n            return element;\n        }\n\t\t\n        #region IList Members\n\n        /// <summary>\n        /// Adds an element to the UIElementCollection\n        /// </summary>\n        int IList.Add(object value)\n        {\n            return Add(Cast(value));\n        }\n\n        /// <summary>\n        /// Determines whether an element is in the UIElementCollection.\n        /// </summary>\n        bool IList.Contains(object value)","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs#L373-L409","documentation":"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.","triggerScenarios":"panel.Children.Add(someNonVisualObject), Insert with a business object or string, or IList.Add through a non-generic reference with a wrong-typed item.","commonSituations":"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.","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."],"exampleFix":"// before\npanel.Children.Add(\"Hello\");\n// after\npanel.Children.Add(new TextBlock { Text = \"Hello\" });","handlingStrategy":"type-guard","validationCode":"if (value is UIElement uiElement) panel.Children.Add(uiElement);","typeGuard":"static bool IsUIElement(object o) => o is UIElement;","tryCatchPattern":"try { panel.Children.Add(value); }\ncatch (ArgumentException ex) { /* value was not a UIElement; wrap or convert it */ }","preventionTips":["Only add UIElement-derived objects to panel Children.","Wrap raw data in TextBlock/ContentPresenter before adding.","Guard IList.Add paths with `is UIElement` checks."],"tags":["wpf","argument-exception","type-mismatch","uielementcollection"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}