{"record":{"id":"d369d88a05829395","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-oldcapture-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, oldCapture.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, oldCapture\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs","lineNumber":535,"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, oldCapture.GetType())); \n                    }\n                }\n\n                if (_stylusCapture != null)\n                {\n                    o = _stylusCapture as DependencyObject;\n                    if (o is UIElement element)\n                    {\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)","sourceCodeStart":517,"sourceCodeEnd":553,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs#L517-L553","documentation":"PointerLogic.UpdateStylusCapture unsubscribes capture-change event handlers from the previous stylus-capture element. If oldCapture is a DependencyObject that is neither UIElement, ContentElement, nor UIElement3D, an InvalidOperationException (SR.Invalid_IInputElement with the concrete type) is thrown, since the element is not a supported IInputElement.","triggerScenarios":"An object that is an IInputElement/DependencyObject but not one of the three supported element types being assigned as stylus capture (via Stylus.Capture) and then released/updated.","commonSituations":"Custom classes implementing IInputElement passed to Stylus.Capture; third-party frameworks with their own input element types used with WPF pointer/stylus capture.","solutions":["Only capture UIElement, ContentElement, or UIElement3D instances","Cast/verify the element type before calling Stylus.Capture","Implement the custom capture element as a subclass of one of the supported types"],"exampleFix":"// before\nStylus.Capture(myCustomInputElement, CaptureMode.Element);\n// after\nif (myCustomInputElement is UIElement || myCustomInputElement is ContentElement || myCustomInputElement is UIElement3D)\n{\n    Stylus.Capture(myCustomInputElement, CaptureMode.Element);\n}","handlingStrategy":"type-guard","validationCode":"static bool CanCapture(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;","typeGuard":"static bool IsSupportedInputElement(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;","tryCatchPattern":"try { Stylus.Capture(element, CaptureMode.Element); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IInputElement\")) { /* reject unsupported element */ }","preventionTips":["Only call Stylus.Capture on UIElement/ContentElement/UIElement3D","Do not implement IInputElement on arbitrary DependencyObjects"],"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-21T21:30:21.729Z"}