{"record":{"id":"9e9b81bf58bd2de5","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-styluscapture-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, _stylusCapture.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, _stylusCapture\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs","lineNumber":561,"sourceCode":"                    {\n                        element.IsEnabledChanged += _captureIsEnabledChangedEventHandler;\n                        element.IsVisibleChanged += _captureIsVisibleChangedEventHandler;\n                        element.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler;\n                    }\n                    else if (o is ContentElement ce)\n                    {\n                        // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements.\n                        ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler;\n                    }\n                    else if (o is UIElement3D element3D)\n                    {\n                        element3D.IsEnabledChanged += _captureIsEnabledChangedEventHandler;\n                        element3D.IsVisibleChanged += _captureIsVisibleChangedEventHandler;\n                        element3D.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler;\n                    }\n                    else\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, _stylusCapture.GetType())); \n                    }\n                }\n\n                // Oddly enough, update the IsStylusCaptureWithin property first.  This is\n                // so any callbacks will see the more-common IsStylusCaptureWithin property\n                // set correctly.\n                UIElement.StylusCaptureWithinProperty.OnOriginValueChanged(oldCapture as DependencyObject, _stylusCapture as DependencyObject, ref _stylusCaptureWithinTreeState);\n\n                // Invalidate the IsStylusCaptured properties.\n                if (oldCapture != null)\n                {\n                    o = oldCapture as DependencyObject;\n                    o.SetValue(UIElement.IsStylusCapturedPropertyKey, false); // Same property for ContentElements\n                }\n\n                if (_stylusCapture != null)\n                {\n                    o = _stylusCapture as DependencyObject;","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs#L543-L579","documentation":"In the same UpdateStylusCapture flow, when subscribing capture-change handlers on the NEW capture element (_stylusCapture), the code throws InvalidOperationException (SR.Invalid_IInputElement with _stylusCapture.GetType()) if the element is not UIElement, ContentElement, or UIElement3D.","triggerScenarios":"Stylus capture being set (Stylus.Capture / Capture) to an unsupported IInputElement type, triggering handler registration on the new capture element.","commonSituations":"Same as the release-side failure: custom IInputElement implementations or foreign framework elements used with stylus capture, often only surfacing later in the pointer logic.","solutions":["Ensure the captured element derives from UIElement/ContentElement/UIElement3D","Type-check before capture and reject unsupported elements at your API boundary","Wrap Stylus.Capture in a helper that validates the element type"],"exampleFix":"// before\nStylus.Capture(externalElement, CaptureMode.SubElement);\n// after\nvar dep = externalElement as DependencyObject;\nif (externalElement is UIElement || externalElement is ContentElement || externalElement is UIElement3D)\n{\n    Stylus.Capture(externalElement, CaptureMode.SubElement);\n}","handlingStrategy":"type-guard","validationCode":"if (!(element is UIElement || element is ContentElement || element is UIElement3D)) throw new NotSupportedException(\"Element type not supported for stylus capture\");","typeGuard":"static bool IsSupportedInputElement(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;","tryCatchPattern":"try { Stylus.Capture(element, CaptureMode.Element); }\ncatch (InvalidOperationException) { /* fall back to mouse capture or reject */ }","preventionTips":["Validate element types at your capture helper boundary","Derive custom interactive elements from UIElement"],"tags":["wpf","stylus","pointer","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}