{"record":{"id":"03f3630d92239fbd","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedparametertype-value-gettype-typeof-03f363","errorCode":null,"errorMessage":"SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(SetterBase))","messagePattern":"SR\\.Format\\(SR\\.UnexpectedParameterType, value\\.GetType\\(\\), typeof\\(SetterBase\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs","lineNumber":394,"sourceCode":"        ///<summary>\n        /// This method is called to Add a Setter object as a child of the Style.\n        /// This method is used primarily by the parser to set style properties and events.\n        ///</summary>\n        ///<param name=\"value\">\n        /// The object to add as a child; it must be a SetterBase subclass.\n        ///</param>\n        void IAddChild.AddChild (Object value)\n        {\n            // Verify Context Access\n            VerifyAccess();\n\n            ArgumentNullException.ThrowIfNull(value);\n\n            SetterBase sb = value as SetterBase;\n\n            if (sb == null)\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(SetterBase)), nameof(value));\n            }\n\n            Setters.Add(sb);\n        }\n\n        ///<summary>\n        /// This method is called by the parser when text appears under the tag in markup.\n        /// As default Styles 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            // Verify Context Access\n            VerifyAccess();\n\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs#L376-L412","documentation":"Style.AddChild (used by the XAML IAddChild path) only accepts SetterBase instances (Setter, EventSetter, Trigger as processed via Setters). Passing any other object as a child of a Style in XAML throws ArgumentException naming the actual and expected types.","triggerScenarios":"Declaring a child element directly under <Style> in XAML that is not a Setter/EventSetter (e.g. a raw <Setter.Property> misplacement, a Trigger placed outside Setters context in code, or adding arbitrary objects via AddChild programmatically).","commonSituations":"Hand-written or templated XAML where a child element is misnested inside <Style> instead of <Style.Setters>; code-generated XAML that adds children via IAddChild; typos like <Style.Triggers> children being routed through AddChild in custom frameworks.","solutions":["Ensure only Setter or EventSetter elements appear as direct children of the Style (Setters collection).","Place Triggers under <Style.Triggers>, not as direct Style children.","If building styles in code, call style.Setters.Add(new Setter(...)) instead of AddChild with arbitrary objects."],"exampleFix":"<!-- before -->\n<Style TargetType=\"Button\">\n  <TextBlock Text=\"oops\" />\n</Style>\n<!-- after -->\n<Style TargetType=\"Button\">\n  <Setter Property=\"Content\" Value=\"ok\" />\n</Style>","handlingStrategy":"type-guard","validationCode":"if (child is not SetterBase)\n    throw new ArgumentException($\"{child.GetType().Name} is not a SetterBase\");","typeGuard":"static bool IsValidStyleChild(object o) => o is SetterBase;","tryCatchPattern":"try { style.AddChild(value); }\ncatch (ArgumentException ex) { log.Error(\"Invalid Style child\", ex); }","preventionTips":["Only place Setter/EventSetter elements directly under a Style in XAML.","Put triggers under Style.Triggers, not as direct children.","Prefer style.Setters.Add(...) over the IAddChild path in code."],"tags":["wpf","style","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"}