dotnet/wpf · error · ArgumentException
SR.Animation_ChildMustBeKeyFrame
Error message
SR.Animation_ChildMustBeKeyFrame
What it means
ColorAnimationUsingKeyFrames.AddChild throws ArgumentException when the child object passed to it is not (and cannot be converted to) a ColorKeyFrame. Key-frame animations only accept key frame children; anything else is rejected. The SR resource key Animation_ChildMustBeKeyFrame carries this message, with the parameter name 'child' attached.
Solutions
- Pass only ColorKeyFrame-derived objects (DiscreteColorKeyFrame, LinearColorKeyFrame, SplineColorKeyFrame, EasingColorKeyFrame) as children
- Wrap raw values in a key frame, e.g. new DiscreteColorKeyFrame(Colors.Red, KeyTime.FromTimeSpan(...))
- Fix the XAML so each child of ColorAnimationUsingKeyFrames is a key frame element
- Use KeyFrames.Add explicitly with the correct key frame type
Example fix
// before animation.AddChild(Colors.Red); // ArgumentException // after animation.AddChild(new DiscreteColorKeyFrame(Colors.Red, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
Defensive patterns
Strategy: type-guard
Validate before calling
if (child is ColorKeyFrame keyFrame)
animation.AddChild(keyFrame);
else
throw new ArgumentException($"Child must be a ColorKeyFrame, got {child?.GetType().Name}"); Type guard
static bool IsColorKeyFrame(object child) => child is ColorKeyFrame;
Try / catch
try { animation.AddChild(child); }
catch (ArgumentException ex) when (ex.ParamName == "child") { /* convert or reject the child */ } Prevention
- Only nest *ColorKeyFrame elements in ColorAnimationUsingKeyFrames in XAML
- Wrap raw values in Discrete/Linear/SplineColorKeyFrame
- Prefer KeyFrames.Add(ColorKeyFrame) for compile-time typing
When it happens
Trigger: Calling AddChild(object) on a ColorAnimationUsingKeyFrames with a value that is not a ColorKeyFrame — e.g. passing a Color, a string, or another animation type as XAML/content child.
Common situations: XAML authoring mistakes where a child element of the key-frame animation is not a DiscreteColorKeyFrame/LinearColorKeyFrame/SplineColorKeyFrame; generated/designer code inserting a wrong child type.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
- ColorKeyFrameCollection
- DecimalKeyFrameCollection
- DoubleKeyFrameCollection
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0bbfa73c3b09ced1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/ColorAnimationUsingKeyFrames.cs:227
WritePostscript();
}
/// <summary>
/// Implemented to allow KeyFrames to be direct children
/// of KeyFrameAnimations in markup.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void AddChild(object child)
{
ColorKeyFrame keyFrame = child as ColorKeyFrame;
if (keyFrame != null)
{
KeyFrames.Add(keyFrame);
}
else
{
throw new ArgumentException(SR.Animation_ChildMustBeKeyFrame, nameof(child));
}
}
/// <summary>
/// Adds a text string as a child of this KeyFrameAnimation.
/// </summary>
/// <param name="childText">
/// The text to add.
/// </param>
/// <remarks>
/// A KeyFrameAnimation does not accept text as a child, so this method will
/// raise an InvalididOperationException unless a derived class has
/// overridden the behavior to add text.
/// </remarks>
/// <exception cref="ArgumentNullException">The childText parameter is
/// null.</exception>
void IAddChild.AddText(string childText)
{View on GitHub (pinned to 81131a70a4)