dotnet/wpf · error · ArgumentException

Animation_ChildMustBeKeyFrame

Animation_ChildMustBeKeyFrame

Error message

SR.Animation_ChildMustBeKeyFrame (Animation_ChildMustBeKeyFrame)

What it means

AddChild was called on a BooleanAnimationUsingKeyFrames (key-frame animation) with an object that is not an IKeyFrame — likely a Boolean. Key-frame animations accept only key frame children (BooleanKeyFrame instances such as DiscreteBooleanKeyFrame); anything else makes the content model invalid and an ArgumentException is thrown.

Solutions

  1. Wrap the value in a key frame: <DiscreteBooleanKeyFrame Value="True" KeyTime="..."/> instead of a bare Boolean child.
  2. In code, use KeyFrames.Add(new DiscreteBooleanKeyFrame(true, KeyTime.FromTimeSpan(ts))) rather than AddChild.
  3. Fix XAML generators/templates to always emit key frame elements as children of XxxAnimationUsingKeyFrames.

Example fix

// before (XAML)
<BooleanAnimationUsingKeyFrames>
  <sys:Boolean>True</sys:Boolean>
</BooleanAnimationUsingKeyFrames>
// after
<BooleanAnimationUsingKeyFrames>
  <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:1"/>
</BooleanAnimationUsingKeyFrames>
Defensive patterns

Strategy: type-guard

Validate before calling

if (child is not IKeyFrame keyFrame)
    throw new ArgumentException("Only key frame children (e.g. DiscreteBooleanKeyFrame) are allowed", nameof(child));

Type guard

static bool IsKeyFrameChild(object child) => child is BooleanKeyFrame;

Try / catch

try { animation.AddChild(child); }
catch (ArgumentException) { animation.KeyFrames.Add(new DiscreteBooleanKeyFrame((bool)child, KeyTime.Uniform)); }

Prevention

When it happens

Trigger: In XAML, nesting a plain <sys:Boolean>true</sys:Boolean> element inside <BooleanAnimationUsingKeyFrames> instead of a key frame element; in code, calling AddChild(true) directly.

Common situations: Hand-editing XAML without the <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:1"/> wrapper; copy-pasting key-frame markup from Double animation samples; tooling that emits child values without key-frame wrappers.

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

Appendix: source

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

            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)