dotnet/wpf · error · InvalidOperationException

SR.Animation_Invalid_DefaultValue

Error message

SR.Animation_Invalid_DefaultValue (destination: {0}, value: {2})

What it means

DoubleAnimation.GetCurrentValueCore throws this InvalidOperationException when the animation is asked to produce a value but the destination value to animate from/to is not a valid animation value for Double (e.g. NaN or Infinity). WPF animations require finite numeric source/destination values to interpolate. The message names the offending parameter ('destination') and the invalid value.

Solutions

  1. Check the value bound or passed as the animation destination; replace NaN/Infinity with a finite number before the animation runs
  2. Validate inputs with double.IsFinite(value) (or !IsNaN && !IsInfinity) before constructing/starting the animation
  3. If the destination comes from layout, wait for layout to complete (e.g. Loaded event) so measured sizes are actual numbers instead of NaN
  4. Wrap animation start in a try/catch for InvalidOperationException and fall back to a static value

Example fix

// before
var anim = new DoubleAnimation(0, double.NaN, TimeSpan.FromSeconds(1));
// after
var target = double.IsFinite(computedValue) ? computedValue : 0;
var anim = new DoubleAnimation(0, target, TimeSpan.FromSeconds(1));
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(to) || double.IsInfinity(to)) throw new ArgumentException("Animation destination must be finite", nameof(to));

Type guard

static bool IsValidAnimationTarget(double v) => double.IsFinite(v);

Try / catch

try { animation.Begin(); } catch (InvalidOperationException ex) when (ex.Message.Contains("Animation_Invalid_DefaultValue") || ex.Message.Contains("destination")) { /* log and use static value */ }

Prevention

When it happens

Trigger: Calling GetCurrentValueCore (directly or via the animation engine) with defaultDestinationValue that fails AnimatedTypeHelpers.IsValidAnimationValueDouble — typically double.NaN or double.PositiveInfinity/NegativeInfinity passed as the default destination value while validateDestination is true.

Common situations: Data-binding an animation To/By property to a computed value that evaluates to NaN (e.g. 0/0 from a binding or a layout measure that returned NaN width); passing NaN as a default value in custom animation code; deserializing animation markup with unset or invalid values.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/DoubleAnimation.cs:324

                    break;
            }

            if (validateOrigin 
                && !AnimatedTypeHelpers.IsValidAnimationValueDouble(defaultOriginValue))
            {
                throw new InvalidOperationException(
                    SR.Format(
                        SR.Animation_Invalid_DefaultValue,
                        this.GetType(),
                        "origin",
                        defaultOriginValue.ToString(CultureInfo.InvariantCulture)));
            }

            if (validateDestination 
                && !AnimatedTypeHelpers.IsValidAnimationValueDouble(defaultDestinationValue))
            {
                throw new InvalidOperationException(
                    SR.Format(
                        SR.Animation_Invalid_DefaultValue,
                        this.GetType(),
                        "destination",
                        defaultDestinationValue.ToString(CultureInfo.InvariantCulture)));
            }


            if (IsCumulative)
            {
                double currentRepeat = (double)(animationClock.CurrentIteration - 1);

                if (currentRepeat > 0.0)
                {
                    Double accumulator = AnimatedTypeHelpers.SubtractDouble(to, from);

                    accumulated = AnimatedTypeHelpers.ScaleDouble(accumulator, currentRepeat);
                }

View on GitHub (pinned to 81131a70a4)