dotnet/wpf · error · ArgumentException

SR.Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame

What it means

MatrixAnimationUsingKeyFrames.AddChild throws ArgumentException when the child supplied via the XAML/IAddChild path is not a MatrixKeyFrame. Matrix key-frame animations only accept MatrixKeyFrame children.

Solutions

  1. Use MatrixKeyFrame-derived children (DiscreteMatrixKeyFrame, LinearMatrixKeyFrame, etc.).
  2. Fix the child element type in XAML to match the animation value type.
  3. In code, add typed MatrixKeyFrame instances via KeyFrames.Add.

Example fix

// before
<MatrixAnimationUsingKeyFrames>
  <LinearDoubleKeyFrame KeyTime="0:0:1" Value="2" />
</MatrixAnimationUsingKeyFrames>
// after
<MatrixAnimationUsingKeyFrames>
  <LinearMatrixKeyFrame KeyTime="0:0:1">
    <Matrix M11="2" M22="2" />
  </LinearMatrixKeyFrame>
</MatrixAnimationUsingKeyFrames>
Defensive patterns

Strategy: validation

Validate before calling

if (child is not MatrixKeyFrame)
    throw new ArgumentException("Children of MatrixAnimationUsingKeyFrames must be MatrixKeyFrame instances", nameof(child));

Type guard

bool IsMatrixKeyFrame(object o) => o is MatrixKeyFrame;

Try / catch

try { animation.AddChild(child); }
catch (ArgumentException ex) when (ex.ParamName == "child") {
    // replace with a MatrixKeyFrame (e.g. LinearMatrixKeyFrame)
}

Prevention

When it happens

Trigger: Declaring a non-MatrixKeyFrame child inside <MatrixAnimationUsingKeyFrames> in XAML, or calling AddChild with an incompatible object.

Common situations: Wrong key frame type in markup (e.g. DoubleKeyFrame in a Matrix animation); refactoring animations between value types without updating children.

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/0dd5a4f0ca0eb60e. Report an issue: GitHub.

Appendix: source

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

            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)