{"record":{"id":"640b7a49dfb77fd8","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-dostyluscapture-gettype-640b7a","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, doStylusCapture\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs","lineNumber":245,"sourceCode":"            {\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            if (doStylusCapture != null && !InputElement.IsValid(element))\n            {\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType()));\n            }\n\n            doStylusCapture?.VerifyAccess();\n\n            bool success = false;\n\n            // The element we are capturing to must be both enabled and visible.\n\n            UIElement e = element as UIElement;\n            if (e != null)\n            {\n                if (e.IsVisible || e.IsEnabled)\n                {\n                    success = true;\n                }\n            }\n            else\n            {","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs#L227-L263","documentation":"WispStylusDevice.Capture (WispStylusDevice.cs:245) throws InvalidOperationException(SR.Invalid_IInputElement) when the element passed as the stylus capture target is a DependencyObject that is not a valid IInputElement (not UIElement, ContentElement, or UIElement3D). The stylus capture API only supports those three element classes.","triggerScenarios":"Calling stylusDevice.Capture(el, mode) with el being a Visual/DependencyObject (e.g. a raw Visual, Visual3D, or custom DependencyObject) rather than a supported IInputElement.","commonSituations":"Grabbing capture on drawing visuals or decorator children held as DependencyObject references; refactored code that widened a variable's type from UIElement to DependencyObject.","solutions":["Cast to / use the concrete UIElement, ContentElement, or UIElement3D instance when capturing.","Check InputElement.IsValid(element) before calling Capture.","Capture an ancestor that is a proper input element instead of the raw visual."],"exampleFix":"// before\nDependencyObject target = myVisual;\nstylusDevice.Capture(target, CaptureMode.Element); // throws\n// after\nif (InputElement.IsValid(myVisual)) stylusDevice.Capture(myVisual, CaptureMode.Element);","handlingStrategy":"type-guard","validationCode":"if (element != null && !InputElement.IsValid(element)) return; // abort capture on unsupported element","typeGuard":"static bool IsCapturable(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;","tryCatchPattern":"try { stylusDevice.Capture(el, mode); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"IInputElement\")) { /* find nearest supported ancestor and capture that */ }","preventionTips":["Type capture-target variables as UIElement rather than DependencyObject/Visual","Check InputElement.IsValid before any capture call","Capture the control, not its raw visual children"],"tags":["wpf","stylus","capture","invalidoperation"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}