{"record":{"id":"cdbc4de0cc2730bb","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-dostyluscapture-gettype","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/Pointer/PointerStylusDevice.cs","lineNumber":552,"sourceCode":"                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()));\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            UIElement e = element as UIElement;\n\n            if ((e?.IsVisible ?? false) || (e?.IsEnabled ?? false))\n            {\n                success = true;\n            }\n            else\n            {\n                ContentElement ce = element as ContentElement;\n\n                if (ce?.IsEnabled ?? false)","sourceCodeStart":534,"sourceCodeEnd":570,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerStylusDevice.cs#L534-L570","documentation":"PointerStylusDevice.Capture requires the element to be a valid input element: a UIElement, ContentElement, or UIElement3D (checked via InputElement.IsValid). If the element is a DependencyObject of any other type, Capture throws InvalidOperationException naming its type. Stylus capture tracking needs the property-change hooks only those element types provide.","triggerScenarios":"Calling StylusDevice.Capture(otherElement, mode) where otherElement is a DependencyObject (e.g. a Visual, a Freezable, or a custom IInputElement) but not UIElement/ContentElement/UIElement3D.","commonSituations":"Capturing on a DrawingVisual (visual but not an input element); capturing on a Grid3D child or non-element node; refactoring code that used mouse capture on visuals into stylus capture.","solutions":["Capture on the nearest ancestor UIElement instead of the raw visual","Derive the capture target from UIElement (or ContentElement/UIElement3D)","Use VisualTreeHelper to walk up to a supported input element before capturing"],"exampleFix":"// before\ndev.Capture(drawingVisual, CaptureMode.Element); // DrawingVisual is not IInputElement\n// after\nvar host = FindAncestor<UIElement>(drawingVisual);\nif (host != null) dev.Capture(host, CaptureMode.Element);","handlingStrategy":"type-guard","validationCode":"if (element == null || InputElement.IsValid(element))\n    dev.Capture(element, mode);","typeGuard":"bool IsValidInputElement(object e) => e == null || InputElement.IsValid(e);","tryCatchPattern":"try { dev.Capture(element, mode); }\ncatch (InvalidOperationException ex) { Log.Warn(\"Capture target not an IInputElement\", ex); }","preventionTips":["Validate with InputElement.IsValid before capture","Capture the nearest UIElement ancestor of visuals","Avoid capturing DrawingVisual or Freezable objects"],"tags":["wpf","stylus","capture","invalid-operation"],"backgroundTag":"type-mismatch","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"}