dotnet/wpf · error · ArgumentException

SR.Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame

What it means

DecimalAnimationUsingKeyFrames.AddChild throws ArgumentException (SR.Animation_ChildMustBeKeyFrame) when the supplied child is not a DecimalKeyFrame. Key-frame animations only accept key frames as children; the parameter name 'child' accompanies the message.

Solutions

  1. Pass only DecimalKeyFrame-derived objects (DiscreteDecimalKeyFrame, LinearDecimalKeyFrame, SplineDecimalKeyFrame)
  2. Wrap the value in a key frame with a KeyTime
  3. Correct the XAML child element type
  4. Use KeyFrames.Add with the proper key frame type explicitly

Example fix

// before
animation.AddChild(5m); // ArgumentException
// after
animation.AddChild(new LinearDecimalKeyFrame(5m, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));
Defensive patterns

Strategy: type-guard

Validate before calling

if (child is DecimalKeyFrame keyFrame)
    animation.AddChild(keyFrame);
else
    throw new ArgumentException($"Child must be a DecimalKeyFrame, got {child?.GetType().Name}");

Type guard

static bool IsDecimalKeyFrame(object child) => child is DecimalKeyFrame;

Try / catch

try { animation.AddChild(child); }
catch (ArgumentException ex) when (ex.ParamName == "child") { /* wrap value in a key frame instead */ }

Prevention

When it happens

Trigger: Calling AddChild with anything other than a DecimalKeyFrame — a raw decimal, string, or a key frame of the wrong element type.

Common situations: XAML mistakes nesting a wrong element inside <DecimalAnimationUsingKeyFrames>; generated code passing the numeric value directly instead of a key frame object.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/50dbd356a278317e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/DecimalAnimationUsingKeyFrames.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)
        {
            DecimalKeyFrame keyFrame = child as DecimalKeyFrame;

            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)