{"record":{"id":"a9d36199be0284ad","repo":"dotnet/wpf","slug":"sr-unexpectedparametertype-decorator","errorCode":null,"errorMessage":"SR.UnexpectedParameterType","messagePattern":"SR\\.UnexpectedParameterType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Decorator.cs","lineNumber":65,"sourceCode":"        //  Public Methods\n        //\n        //-------------------------------------------------------------------\n\n        #region Public Methods\n\n        ///<summary>\n        /// This method is called to Add the object as a child of the Decorator.  This method is used primarily\n        /// by the parser; a more direct way of adding a child to a Decorator 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            if (!(value is UIElement))\n            {\n                throw new ArgumentException (SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(UIElement)), nameof(value));\n            }\n\n            if (this.Child != null)\n            {\n                throw new ArgumentException(SR.Format(SR.CanOnlyHaveOneChild, this.GetType(), value.GetType()));\n            }\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        /// As Decorators do not support text, calling this method has no effect if the text\n        /// is all whitespace.  For non-whitespace text, throw an exception.\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to add as a child.\n        ///</param> ","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Decorator.cs#L47-L83","documentation":"Decorator's explicit IAddChild.AddChild implementation rejects any value that is not a UIElement. WPF XAML parsing calls this method when assigning the child element, so passing a non-visual object as the Decorator's child triggers ArgumentException with the UnexpectedParameterType message. Decorator can only host visual children, hence the strict type requirement.","triggerScenarios":"Calling IAddChild.AddChild explicitly with a non-UIElement object, or XAML markup that places a non-UIElement (e.g. a string or POCO) directly inside a Border, Viewbox, or other Decorator-derived element.","commonSituations":"XAML typos placing plain text or data objects as the direct child of a Border; programmatic code casting errors where an object is assumed to be a UIElement but is not; binding a non-visual object into Content where the template uses a Decorator.","solutions":["Ensure the object passed as the Decorator child derives from System.Windows.UIElement","Wrap non-visual content in a UIElement host (e.g. ContentControl or TextBlock) before adding","Fix the XAML so only element syntax (not raw text/data) appears inside the Decorator","If a data item is intended, bind it to Content of a ContentControl instead of adding directly"],"exampleFix":"// before\nborder.AddChild(new MyViewModel());\n// after\nborder.AddChild(new ContentControl { Content = new MyViewModel() });","handlingStrategy":"type-guard","validationCode":"if (!(value is UIElement)) throw new ArgumentException(nameof(value));","typeGuard":"bool IsValidChild(object v) => v is UIElement;","tryCatchPattern":"try { ((IAddChild)decorator).AddChild(value); }\ncatch (ArgumentException ex) { /* value was not a UIElement */ }","preventionTips":["Only pass UIElement-derived objects as Decorator children","Prefer the strongly-typed Child property over IAddChild.AddChild","In XAML, place only element syntax inside Decorator-derived tags"],"tags":["wpf","xaml","argument-exception"],"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"}