{"record":{"id":"6884b4122d1d42cb","repo":"dotnet/wpf","slug":"sr-animation-childmustbekeyframe-rectanimationusingkeyframes","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/RectAnimationUsingKeyFrames.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            RectKeyFrame keyFrame = child as RectKeyFrame;\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/RectAnimationUsingKeyFrames.cs#L209-L245","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"// before\n<RectAnimationUsingKeyFrames>\n  <LinearDoubleKeyFrame Value=\"100\" KeyTime=\"0:0:1\" /> <!-- wrong type -->\n</RectAnimationUsingKeyFrames>\n// after\n<RectAnimationUsingKeyFrames>\n  <LinearRectKeyFrame Value=\"new Rect(0,0,100,100)\" KeyTime=\"0:0:1\" />\n</RectAnimationUsingKeyFrames>","handlingStrategy":"type-guard","validationCode":"if (child is RectKeyFrame kf) keyFrames.KeyFrames.Add(kf); else throw new ArgumentException(nameof(child));","typeGuard":"bool IsRectKeyFrame(object o) => o is RectKeyFrame;","tryCatchPattern":"try { keyFrameAnimation.AddChild(child); } catch (ArgumentException) { /* child was not a RectKeyFrame; use correct key frame type */ }","preventionTips":["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"],"tags":["wpf","animation","xaml","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}