{"record":{"id":"c5cf0b04d8a22ef4","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-element-gettype-c5cf0b","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, element.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, element\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/TouchDevice.cs","lineNumber":331,"sourceCode":"        {\n            VerifyAccess();\n\n            // If the element is null or captureMode is None, ensure\n            // that the other parameter is consistent.\n            if ((element == null) || (captureMode == CaptureMode.None))\n            {\n                element = null;\n                captureMode = CaptureMode.None;\n            }\n\n            UIElement uiElement;\n            ContentElement contentElement;\n            UIElement3D uiElement3D;\n            CastInputElement(element, out uiElement, out contentElement, out uiElement3D);\n\n            if ((element != null) && (uiElement == null) && (contentElement == null) && (uiElement3D == null))\n            {\n                throw new ArgumentException(SR.Format(SR.Invalid_IInputElement, element.GetType()), nameof(element));\n            }\n\n            if (_captured != element)\n            {\n                // Ensure that the new element is visible and enabled\n                if ((element == null) ||\n                    (((uiElement != null) && uiElement.IsVisible && uiElement.IsEnabled) ||\n                    ((contentElement != null) && contentElement.IsEnabled) ||\n                    ((uiElement3D != null) && uiElement3D.IsVisible && uiElement3D.IsEnabled)))\n                {\n                    IInputElement oldCapture = _captured;\n                    _captured = element;\n                    _captureMode = captureMode;\n\n                    UIElement oldUIElement;\n                    ContentElement oldContentElement;\n                    UIElement3D oldUIElement3D;\n                    CastInputElement(oldCapture, out oldUIElement, out oldContentElement, out oldUIElement3D);","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/TouchDevice.cs#L313-L349","documentation":"TouchDevice.Capture accepts only elements that implement WPF's IInputElement contract as concrete UIElement, ContentElement, or UIElement3D instances. Before capturing, CastInputElement tries to cast the argument to those three concrete types; if the element is non-null yet none of the casts succeed, the device cannot route touch events to it, so an ArgumentException naming the 'element' parameter is thrown. This guards the invariant that captured objects participate in WPF input hit-testing and event routing.","triggerScenarios":"Calling touchDevice.Capture(obj) where obj is non-null but is not a UIElement, ContentElement, or UIElement3D — e.g. passing a raw DependencyObject, a custom control that implements IInputElement but does not derive from the three supported classes, a Window content proxy, or a mistakenly wrapped/partial-class instance.","commonSituations":"Custom framework code implementing IInputElement directly instead of subclassing UIElement; passing view-model or wrapper objects to Capture; interop scenarios where a foreign element type from another framework is passed; typos that pass the wrong variable (e.g. a DataContext instead of the visual element).","solutions":["Pass a real UIElement (or ContentElement/UIElement3D) instance to TouchDevice.Capture — typically the element that received the TouchDown event (e.g. args.Source or args.OriginalSource as UIElement).","If you have a custom element class, derive it from UIElement rather than implementing IInputElement manually.","Before calling, verify the target with the same check the API performs: element is null or is UIElement/ContentElement/UIElement3D."],"exampleFix":"// before\nobject source = e.OriginalSource;\ntouchDevice.Capture(source); // ArgumentException if source is not a supported element\n\n// after\nvar element = e.OriginalSource as UIElement;\ntouchDevice.Capture(element); // null means 'release capture'; valid UIElement means capture","handlingStrategy":"type-guard","validationCode":"bool canCapture(object element) =>\n    element == null || element is UIElement || element is ContentElement || element is UIElement3D;","typeGuard":"bool IsInputElement(object o, out IInputElement input) =>\n    (input = o as UIElement ?? (IInputElement)(o as ContentElement) ?? (o as UIElement3D)) != null || o == null;","tryCatchPattern":"try { touchDevice.Capture(element); }\ncatch (ArgumentException ex) when (ex.ParamName == \"element\")\n{\n    // fall back to capturing the original source element\n    touchDevice.Capture(e.OriginalSource as UIElement);\n}","preventionTips":["Only capture elements obtained from touch event args (e.OriginalSource / e.Source).","Derive custom input elements from UIElement, never implement IInputElement directly.","Guard with `is UIElement` before every Capture call."],"tags":["wpf","touch","argument-exception","invalid-argument-value"],"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-21T21:30:21.729Z"}