dotnet/wpf · error · ArgumentException
SR.Animation_ChildMustBeKeyFrame
Error message
SR.Animation_ChildMustBeKeyFrame
What it means
RectAnimationUsingKeyFrames.AddChild throws ArgumentException (SR.Animation_ChildMustBeKeyFrame) when a child added in XAML or code is not a RectKeyFrame. Key-frame animations only accept key frame children; anything else (another Timeline, a string, etc.) cannot be added to the KeyFrames collection.
Solutions
- Add only RectKeyFrame-derived children: LinearRectKeyFrame, DiscreteRectKeyFrame, SplineRectKeyFrame, EasingRectKeyFrame
- Fix XAML so children use the correct key frame type matching the animation's value type
- If adding programmatically, call KeyFrames.Add(keyFrame) directly after verifying the type
Example fix
// before <RectAnimationUsingKeyFrames> <LinearDoubleKeyFrame Value="100" KeyTime="0:0:1" /> <!-- wrong type --> </RectAnimationUsingKeyFrames> // after <RectAnimationUsingKeyFrames> <LinearRectKeyFrame Value="new Rect(0,0,100,100)" KeyTime="0:0:1" /> </RectAnimationUsingKeyFrames>
Defensive patterns
Strategy: type-guard
Validate before calling
if (child is RectKeyFrame kf) keyFrames.KeyFrames.Add(kf); else throw new ArgumentException(nameof(child));
Type guard
bool IsRectKeyFrame(object o) => o is RectKeyFrame;
Try / catch
try { keyFrameAnimation.AddChild(child); } catch (ArgumentException) { /* child was not a RectKeyFrame; use correct key frame type */ } Prevention
- Only nest *RectKeyFrame elements inside RectAnimationUsingKeyFrames in XAML
- Verify key frame types match the animation's value type
- Use KeyFrames.Add directly in code instead of AddChild
When it happens
Trigger: Calling AddChild(child) with an object that is not a RectKeyFrame — e.g. adding a DoubleAnimation or arbitrary object as a child of RectAnimationUsingKeyFrames in XAML or via the IAddChild path.
Common situations: XAML authoring mistakes: nesting the wrong animation type inside a key-frame animation, or copy-pasting children between animations of different target types (e.g. DoubleKeyFrame inside a Rect key-frame animation).
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
- Animation_AnimationTimelineTypeMismatch
- Animation_ChildMustBeKeyFrame
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6884b4122d1d42cb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/RectAnimationUsingKeyFrames.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)
{
RectKeyFrame keyFrame = child as RectKeyFrame;
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)