dotnet/wpf · error · InvalidEnumArgumentException

captureMode

Error message

captureMode

What it means

PointerStylusDevice.Capture validates the captureMode parameter and throws InvalidEnumArgumentException when it is not one of CaptureMode.None, CaptureMode.Element, or CaptureMode.SubTree. This is a standard argument-validation guard: an out-of-range enum value was cast into the parameter.

Solutions

  1. Pass only valid CaptureMode members: None, Element, or SubTree
  2. Validate/cast any int-based mode with Enum.IsDefined(typeof(CaptureMode), value) before calling
  3. Check that no other enum type's value is being passed in place of CaptureMode

Example fix

// before
dev.Capture(elem, (CaptureMode)42);
// after
var mode = (CaptureMode)42;
if (!Enum.IsDefined(typeof(CaptureMode), mode)) return;
dev.Capture(elem, mode);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(CaptureMode), mode)) throw new ArgumentException($"Invalid CaptureMode: {mode}");
dev.Capture(element, mode);

Type guard

bool IsValidCaptureMode(CaptureMode m) => m is CaptureMode.None or CaptureMode.Element or CaptureMode.SubTree;

Try / catch

try { dev.Capture(element, mode); }
catch (InvalidEnumArgumentException ex) { Log.Error("Bad CaptureMode", ex); }

Prevention

When it happens

Trigger: Calling StylusDevice.Capture(element, mode) with a CaptureMode value cast from an arbitrary int, or with an enum member that does not exist in CaptureMode (e.g. from a different enum type or a bad cast).

Common situations: Storing capture mode in an int config field and casting it; binding a UI ComboBox value that is not a CaptureMode member; using reflection or interop to pass an invalid integer.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerStylusDevice.cs:534

            }
        }

        #endregion

        #region StylusDeviceBase Functions

        /// <summary>
        ///     Captures the stylus to a particular element.
        /// </summary>
        internal override bool Capture(IInputElement element, CaptureMode captureMode)
        {
            int timeStamp = Environment.TickCount;

            VerifyAccess();

            if (!(captureMode == CaptureMode.None || captureMode == CaptureMode.Element || captureMode == CaptureMode.SubTree))
            {
                throw new System.ComponentModel.InvalidEnumArgumentException("captureMode", (int)captureMode, typeof(CaptureMode));
            }

            if (element == null)
            {
                captureMode = CaptureMode.None;
            }

            if (captureMode == CaptureMode.None)
            {
                element = null;
            }

            // Validate that element is either a UIElement, a ContentElement or a UIElement3D.
            DependencyObject doStylusCapture = element as DependencyObject;

            if (doStylusCapture != null && !InputElement.IsValid(element))
            {
                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType()));

View on GitHub (pinned to 81131a70a4)