dotnet/wpf · error · ArgumentException

SR.Format(SR.Animation_AnimationTimelineTypeMismatch…

Error message

SR.Format(SR.Animation_AnimationTimelineTypeMismatch, animation.GetType(), dp.Name, dp.PropertyType)

What it means

Thrown by ContentElement.BeginAnimation when a non-null animation's type is invalid for the target property (AnimationStorage.IsAnimationValid(dp, animation) fails). The AnimationTimeline's value type does not match dp.PropertyType, producing an ArgumentException that names the animation type, property, and expected type.

Solutions

  1. Use the animation class matching dp.PropertyType (DoubleAnimation, ColorAnimation, PointAnimation, ThicknessAnimation, etc.).
  2. Read the exception message: it lists animation type, dp.Name, and dp.PropertyType — swap the animation accordingly.
  3. Validate with AnimationStorage.IsAnimationValid(dp, animation) before calling BeginAnimation.
  4. In generic helpers, resolve the animation type from dp.PropertyType at runtime.

Example fix

// before
contentEl.BeginAnimation(TextElement.FontSizeProperty, new ColorAnimation(Colors.Red, TimeSpan.FromSeconds(1)));
// after
contentEl.BeginAnimation(TextElement.FontSizeProperty, new DoubleAnimation(12, 24, TimeSpan.FromSeconds(1)));
Defensive patterns

Strategy: type-guard

Validate before calling

if (animation != null && !AnimationStorage.IsAnimationValid(dp, animation))
    throw new InvalidOperationException($"{animation.GetType().Name} cannot animate {dp.Name} ({dp.PropertyType})");

Type guard

bool AnimationMatches<TValue>(DependencyProperty dp, AnimationTimeline a) where TValue : AnimationTimeline => a is TValue && dp.PropertyType == typeof(TValue);

Try / catch

try { el.BeginAnimation(dp, animation); }
catch (ArgumentException ex) when (ex.ParamName == nameof(animation)) { log.Error("Animation type mismatch", ex); }

Prevention

When it happens

Trigger: Calling BeginAnimation(dp, animation) with an AnimationTimeline whose output type mismatches dp.PropertyType — e.g. ColorAnimation applied to a double-typed property.

Common situations: Reusing a single animation instance for properties of differing types; type changes during refactoring leaving stale animation classes; generic helper methods constructing the wrong animation type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/ContentElement.cs:142

        /// and the property value will revert back to its base value.</para>
        /// </param>
        /// <param name="handoffBehavior">
        /// Specifies how the new animation should interact with any current
        /// animations already affecting the property value.
        /// </param>
        public void BeginAnimation(DependencyProperty dp, AnimationTimeline animation, HandoffBehavior handoffBehavior)
        {
            ArgumentNullException.ThrowIfNull(dp);

            if (!AnimationStorage.IsPropertyAnimatable(this, dp))
            {
                throw new ArgumentException(SR.Format(SR.Animation_DependencyPropertyIsNotAnimatable, dp.Name, this.GetType()), nameof(dp));
            }

            if (animation != null
                && !AnimationStorage.IsAnimationValid(dp, animation))
            {
                throw new ArgumentException(SR.Format(SR.Animation_AnimationTimelineTypeMismatch, animation.GetType(), dp.Name, dp.PropertyType), nameof(animation));
            }

            if (!HandoffBehaviorEnum.IsDefined(handoffBehavior))
            {
                throw new ArgumentException(SR.Animation_UnrecognizedHandoffBehavior);
            }

            if (IsSealed)
            {
                throw new InvalidOperationException(SR.Format(SR.IAnimatable_CantAnimateSealedDO, dp, this.GetType()));
            }

            AnimationStorage.BeginAnimation(this, dp, animation, handoffBehavior);
        }

        /// <summary>
        /// Returns true if any properties on this DependencyObject have a
        /// persistent animation or the object has one or more clocks associated

View on GitHub (pinned to 81131a70a4)