dotnet/wpf · error · InvalidOperationException

SR.Animation_Invalid_DefaultValue

Error message

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

What it means

Int16Animation.GetCurrentValueCore throws this InvalidOperationException when the origin (from) value is not a valid animation value for Int16 per AnimatedTypeHelpers.IsValidAnimationValueInt16. For integer types this effectively guards against invalid default values being used as the interpolation source. The message names parameter 'origin' and the invalid value.

Solutions

  1. Clamp/validate the origin value to the Int16 range before starting the animation
  2. Check the bound source property for validity (e.g. short.MinValue..short.MaxValue) prior to animation
  3. Wrap animation setup in try/catch for InvalidOperationException and substitute a safe default

Example fix

// before
short from = unchecked((short)someInt); // may be invalid/overflowed
var anim = new Int16Animation(from, to, duration);
// after
if (someInt >= short.MinValue && someInt <= short.MaxValue)
    var anim = new Int16Animation((short)someInt, to, duration);
Defensive patterns

Strategy: validation

Validate before calling

if (from < short.MinValue || from > short.MaxValue) throw new ArgumentOutOfRangeException(nameof(from));

Type guard

static bool IsValidShortValue(int v) => v >= short.MinValue && v <= short.MaxValue;

Try / catch

try { animation.Begin(); } catch (InvalidOperationException ex) when (ex.Message.Contains("origin")) { /* fall back to a valid default origin */ }

Prevention

When it happens

Trigger: GetCurrentValueCore invoked with defaultOriginValue failing IsValidAnimationValueInt16 while validateOrigin is true — an invalid default origin value supplied to an Int16Animation.

Common situations: Binding an animation From value to data that yields an out-of-range/invalid short; custom animation code passing unchecked defaults; round-tripping values that overflow Int16.

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

Appendix: source

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

                    if (IsAdditive)
                    {
                        foundation = defaultOriginValue;
                        validateOrigin = true;
                    }

                    break;

                default:

                    Debug.Fail("Unknown animation type.");

                    break;
            }

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

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

View on GitHub (pinned to 81131a70a4)