{"record":{"id":"b3791c70b787920f","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedparametertype-value-gettype-typeof","errorCode":null,"errorMessage":"SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement))","messagePattern":"SR\\.Format\\(SR\\.UnexpectedParameterType, value\\.GetType\\(\\), typeof\\(UIElement\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs","lineNumber":59,"sourceCode":"        #endregion Constructors\n\n\n        ///<summary>\n        /// This method is called to Add the object as a child.  This method is used primarily\n        /// by the parser; a more direct way of adding a child is to use the <see cref=\"Child\" />\n        /// property.\n        ///</summary>\n        ///<param name=\"value\">\n        /// The object to add as a child; it must be a UIElement.\n        ///</param>\n        void IAddChild.AddChild (Object value)\n        {\n            // keeping consistent with other elements:  adding null is a no-op.\n            if (value == null)\n                return;\n\n            if (!(value is UIElement))\n                throw new ArgumentException (SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));\n\n            if (this.Child != null)\n                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));\n\n            this.Child = (UIElement)value;\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// Calling this method has no effect if text is just whitespace.  If text is not\n        /// just whitespace, throw an exception.\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);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AdornedElementPlaceholder.cs#L41-L77","documentation":"AdornedElementPlaceholder.AddChild throws this ArgumentException because the element's only valid child is a UIElement. The parser/IAddChild pipeline passes the added child here, and any other object type (string handled separately, arbitrary objects) is rejected.","triggerScenarios":"Adding a non-UIElement child via XAML content or IAddChild.AddChild (e.g. a raw string node routed to AddChild, or a non-visual object from markup extensions).","commonSituations":"Placing text or non-visual content directly inside <AdornedElementPlaceholder> in XAML; programmatic AddChild calls with wrong-typed objects.","solutions":["Only place a UIElement (e.g. a Panel, Border, Control) as the child of AdornedElementPlaceholder in XAML.","Wrap non-UIElement content in a container that hosts it (e.g. a TextBlock for text).","Guard programmatic AddChild calls with `if (value is UIElement)`.","Note null children are silently ignored, so no guard is needed for null."],"exampleFix":"<!-- before -->\n<AdornedElementPlaceholder Name=\"ph\">Some text</AdornedElementPlaceholder>\n<!-- after -->\n<AdornedElementPlaceholder Name=\"ph\">\n  <TextBlock Text=\"Some text\"/>\n</AdornedElementPlaceholder>","handlingStrategy":"type-guard","validationCode":"if (value != null && !(value is UIElement)) throw new ArgumentException(\"AdornedElementPlaceholder child must be a UIElement\");","typeGuard":"bool IsValidChild(object o) => o == null || o is UIElement;","tryCatchPattern":"try { placeholder.AddChild(value); } catch (ArgumentException) { /* wrap value in a UIElement container */ }","preventionTips":["Only put UIElement content inside AdornedElementPlaceholder in XAML","Wrap text or non-visual content in TextBlock/Border containers"],"tags":["wpf","argument","xaml","type-mismatch"],"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"}