dotnet/wpf · error · ArgumentException
SR.Format(SR.UnexpectedParameterType, value.GetType()…
Error message
SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(SetterBase))
What it means
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.
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.
Example fix
<!-- before --> <Style TargetType="Button"> <TextBlock Text="oops" /> </Style> <!-- after --> <Style TargetType="Button"> <Setter Property="Content" Value="ok" /> </Style>
Defensive patterns
Strategy: type-guard
Validate before calling
if (child is not SetterBase)
throw new ArgumentException($"{child.GetType().Name} is not a SetterBase"); Type guard
static bool IsValidStyleChild(object o) => o is SetterBase;
Try / catch
try { style.AddChild(value); }
catch (ArgumentException ex) { log.Error("Invalid Style child", ex); } Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.Animation_ChildMustBeKeyFrame
- SR.ChildHasWrongType (formatted with type name…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/03f3630d92239fbd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Style.cs:394
///<summary>
/// This method is called to Add a Setter object as a child of the Style.
/// This method is used primarily by the parser to set style properties and events.
///</summary>
///<param name="value">
/// The object to add as a child; it must be a SetterBase subclass.
///</param>
void IAddChild.AddChild (Object value)
{
// Verify Context Access
VerifyAccess();
ArgumentNullException.ThrowIfNull(value);
SetterBase sb = value as SetterBase;
if (sb == null)
{
throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(SetterBase)), nameof(value));
}
Setters.Add(sb);
}
///<summary>
/// This method is called by the parser when text appears under the tag in markup.
/// As default Styles do not support text, calling this method has no effect.
///</summary>
///<param name="text">
/// Text to add as a child.
///</param>
void IAddChild.AddText (string text)
{
// Verify Context Access
VerifyAccess();
XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);View on GitHub (pinned to 81131a70a4)