dotnet/wpf · error · InvalidEnumArgumentException

unit

Error message

unit

What it means

The StylusPointPropertyInfo constructor validates the unit parameter via StylusPointPropertyUnitHelper.IsDefined and throws InvalidEnumArgumentException when the unit value is not a defined StylusPointPropertyUnit member. The base constructor already guaranteed stylusPointProperty is non-null, so this error is specifically about an out-of-range/undefined enum value (message string 'unit' is the parameter name).

Solutions

  1. Use only defined members: StylusPointPropertyUnit.None, Inches, Centimeters, Degrees, Radians.
  2. Validate/sanitize external unit codes with Enum.IsDefined(typeof(StylusPointPropertyUnit), value) before casting, defaulting to None otherwise.
  3. Map unsupported device units to the closest defined WPF unit (typically None or Inches) and record the raw value separately.

Example fix

// before
var unit = (StylusPointPropertyUnit)rawDeviceUnit; // may be undefined
var info = new StylusPointPropertyInfo(prop, min, max, unit, res);
// after
var unit = Enum.IsDefined(typeof(StylusPointPropertyUnit), rawDeviceUnit)
    ? (StylusPointPropertyUnit)rawDeviceUnit
    : StylusPointPropertyUnit.None;
var info = new StylusPointPropertyInfo(prop, min, max, unit, res);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(StylusPointPropertyUnit), unitValue)) unitValue = (int)StylusPointPropertyUnit.None;

Type guard

static bool IsDefinedUnit(StylusPointPropertyUnit u) => u is StylusPointPropertyUnit.None or StylusPointPropertyUnit.Inches or StylusPointPropertyUnit.Centimeters or StylusPointPropertyUnit.Degrees or StylusPointPropertyUnit.Radians;

Try / catch

try { var info = new StylusPointPropertyInfo(prop, min, max, unit, res); }
catch (InvalidEnumArgumentException) { info = new StylusPointPropertyInfo(prop, min, max, StylusPointPropertyUnit.None, res); }

Prevention

When it happens

Trigger: Calling new StylusPointPropertyInfo(prop, min, max, (StylusPointPropertyUnit)someInt, resolution) where someInt is not a defined enum value, e.g. a value read from untrusted device data or a cast of an unrelated enum.

Common situations: Building property infos from raw device/unit codes where the device reports a unit WPF does not define; casting an int from a config file or P/Invoke structure directly into StylusPointPropertyUnit.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPointPropertyInfo.cs:50

            _resolution = info.Resolution;
            _unit = info.Unit;
}

        /// <summary>
        /// StylusPointProperty
        /// </summary>
        /// <param name="stylusPointProperty"></param>
        /// <param name="minimum">minimum</param>
        /// <param name="maximum">maximum</param>
        /// <param name="unit">unit</param>
        /// <param name="resolution">resolution</param>
        public StylusPointPropertyInfo(StylusPointProperty stylusPointProperty, int minimum, int maximum, StylusPointPropertyUnit unit, float resolution)
            : base(stylusPointProperty) //base checks for null
        {
            // validate unit
            if (!StylusPointPropertyUnitHelper.IsDefined(unit))
            {
                throw new InvalidEnumArgumentException("unit", (int)unit, typeof(StylusPointPropertyUnit));
            }

            // validate min/max
            if (maximum < minimum)
            {
                throw new ArgumentException(SR.Stylus_InvalidMax, nameof(maximum));
            }

            // validate resolution
            if (resolution < 0.0f)
            {
                throw new ArgumentException(SR.InvalidStylusPointPropertyInfoResolution, nameof(resolution));
            }

            _min = minimum;
            _max = maximum;
            _resolution = resolution;
            _unit = unit;

View on GitHub (pinned to 81131a70a4)