{"record":{"id":"4aa784f4990ae299","repo":"dotnet/wpf","slug":"capturemode","errorCode":null,"errorMessage":"captureMode","messagePattern":"captureMode","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerStylusDevice.cs","lineNumber":534,"sourceCode":"            }\n        }\n\n        #endregion\n\n        #region StylusDeviceBase Functions\n\n        /// <summary>\n        ///     Captures the stylus to a particular element.\n        /// </summary>\n        internal override bool Capture(IInputElement element, CaptureMode captureMode)\n        {\n            int timeStamp = Environment.TickCount;\n\n            VerifyAccess();\n\n            if (!(captureMode == CaptureMode.None || captureMode == CaptureMode.Element || captureMode == CaptureMode.SubTree))\n            {\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"captureMode\", (int)captureMode, typeof(CaptureMode));\n            }\n\n            if (element == null)\n            {\n                captureMode = CaptureMode.None;\n            }\n\n            if (captureMode == CaptureMode.None)\n            {\n                element = null;\n            }\n\n            // Validate that element is either a UIElement, a ContentElement or a UIElement3D.\n            DependencyObject doStylusCapture = element as DependencyObject;\n\n            if (doStylusCapture != null && !InputElement.IsValid(element))\n            {\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType()));","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerStylusDevice.cs#L516-L552","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass only valid CaptureMode members: None, Element, or SubTree","Validate/cast any int-based mode with Enum.IsDefined(typeof(CaptureMode), value) before calling","Check that no other enum type's value is being passed in place of CaptureMode"],"exampleFix":"// before\ndev.Capture(elem, (CaptureMode)42);\n// after\nvar mode = (CaptureMode)42;\nif (!Enum.IsDefined(typeof(CaptureMode), mode)) return;\ndev.Capture(elem, mode);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(CaptureMode), mode)) throw new ArgumentException($\"Invalid CaptureMode: {mode}\");\ndev.Capture(element, mode);","typeGuard":"bool IsValidCaptureMode(CaptureMode m) => m is CaptureMode.None or CaptureMode.Element or CaptureMode.SubTree;","tryCatchPattern":"try { dev.Capture(element, mode); }\ncatch (InvalidEnumArgumentException ex) { Log.Error(\"Bad CaptureMode\", ex); }","preventionTips":["Never cast raw ints to CaptureMode","Use Enum.IsDefined before casting","Pass CaptureMode members directly from code, not from untyped config"],"tags":["wpf","stylus","capture","enum-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}