dotnet/wpf · error · InvalidOperationException

SR.Animation_Invalid_DefaultValue

Error message

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

What it means

ColorAnimation.GetCurrentValueCore throws InvalidOperationException when the animation is asked to validate its destination value and that value is not a valid animation value for the Color type (e.g. it is Color.Empty or otherwise unusable for interpolation). The library throws because animating to/from an invalid color would produce undefined interpolation results. The message names the animation type, the offending role (destination) and the value.

Solutions

  1. Set an explicit To (and/or From) value on the ColorAnimation so it does not rely on the destination default
  2. Initialize the animated property to a concrete Color (e.g. Colors.Transparent) instead of Color.Empty
  3. Ensure the property being animated is actually a Color-typed animation-compatible property
  4. Wrap animation setup so the target property's current value is checked before calling BeginAnimation

Example fix

// before
var anim = new ColorAnimation(); // no To, destination may be Color.Empty
rect.BeginAnimation(SolidColorBrush.ColorProperty, anim);
// after
var anim = new ColorAnimation(Colors.Red, Colors.Blue, TimeSpan.FromSeconds(1));
rect.BeginAnimation(SolidColorBrush.ColorProperty, anim);
Defensive patterns

Strategy: validation

Validate before calling

if (targetBrush.Color == Color.Empty || targetBrush.Color == default)
    anim.To = anim.To ?? Colors.Transparent; // provide explicit destination
else
    targetBrush.BeginAnimation(SolidColorBrush.ColorProperty, anim);

Type guard

static bool HasValidColor(Color c) => c != Color.Empty && AnimatedTypeHelpers.IsValidAnimationValueColor(c);

Try / catch

try { target.BeginAnimation(SolidColorBrush.ColorProperty, anim); }
catch (InvalidOperationException ex) when (ex.Message.Contains("destination")) { /* log and set static value instead */ target.SetValue(SolidColorBrush.ColorProperty, anim.To ?? Colors.Transparent); }

Prevention

When it happens

Trigger: Calling GetCurrentValueCore (directly or via the animation clock) with defaultDestinationValue that fails AnimatedTypeHelpers.IsValidAnimationValueColor while validateDestination is true — typically when the animated dependency property's current value is Color.Empty or unset and no explicit To value is supplied.

Common situations: Animating a Brush/Color property whose base value was never initialized (default Color.Empty); using From/To/By defaults where the target property has no valid current color; code that assigns Color.Empty programmatically before the animation starts.

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

Appendix: source

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

                    break;
            }

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

            if (validateDestination 
                && !AnimatedTypeHelpers.IsValidAnimationValueColor(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)
                {
                    Color accumulator = AnimatedTypeHelpers.SubtractColor(to, from);

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

View on GitHub (pinned to 81131a70a4)