{"record":{"id":"0bbfa73c3b09ced1","repo":"dotnet/wpf","slug":"sr-animation-childmustbekeyframe-0bbfa7","errorCode":null,"errorMessage":"SR.Animation_ChildMustBeKeyFrame","messagePattern":"SR\\.Animation_ChildMustBeKeyFrame","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/ColorAnimationUsingKeyFrames.cs","lineNumber":227,"sourceCode":"            WritePostscript();\n        }\n\n        /// <summary>\n        /// Implemented to allow KeyFrames to be direct children\n        /// of KeyFrameAnimations in markup.\n        /// </summary>\n        [EditorBrowsable(EditorBrowsableState.Advanced)]\n        protected virtual void AddChild(object child)\n        {\n            ColorKeyFrame keyFrame = child as ColorKeyFrame;\n\n            if (keyFrame != null)\n            {\n                KeyFrames.Add(keyFrame);\n            }\n            else\n            {        \n                throw new ArgumentException(SR.Animation_ChildMustBeKeyFrame, nameof(child));\n            }\n        }\n\n        /// <summary>\n        /// Adds a text string as a child of this KeyFrameAnimation.\n        /// </summary>\n        /// <param name=\"childText\">\n        /// The text to add.\n        /// </param>\n        /// <remarks>\n        /// A KeyFrameAnimation does not accept text as a child, so this method will\n        /// raise an InvalididOperationException unless a derived class has\n        /// overridden the behavior to add text.\n        /// </remarks>\n        /// <exception cref=\"ArgumentNullException\">The childText parameter is\n        /// null.</exception>\n        void IAddChild.AddText(string childText)\n        {","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/ColorAnimationUsingKeyFrames.cs#L209-L245","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nanimation.AddChild(Colors.Red); // ArgumentException\n// after\nanimation.AddChild(new DiscreteColorKeyFrame(Colors.Red, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))));","handlingStrategy":"type-guard","validationCode":"if (child is ColorKeyFrame keyFrame)\n    animation.AddChild(keyFrame);\nelse\n    throw new ArgumentException($\"Child must be a ColorKeyFrame, got {child?.GetType().Name}\");","typeGuard":"static bool IsColorKeyFrame(object child) => child is ColorKeyFrame;","tryCatchPattern":"try { animation.AddChild(child); }\ncatch (ArgumentException ex) when (ex.ParamName == \"child\") { /* convert or reject the child */ }","preventionTips":["Only nest *ColorKeyFrame elements in ColorAnimationUsingKeyFrames in XAML","Wrap raw values in Discrete/Linear/SplineColorKeyFrame","Prefer KeyFrames.Add(ColorKeyFrame) for compile-time typing"],"tags":["wpf","animation","keyframes","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}