{"record":{"id":"ba387edd74dbfc3f","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-eltdo-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, eltDO.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, eltDO\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs","lineNumber":299,"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 elt is either a UIElement, a ContentElement or a UIElement3D.\n            DependencyObject eltDO = element as DependencyObject;\n            if (eltDO != null && !InputElement.IsValid(element))\n            {\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, eltDO.GetType()));\n            }\n\n            bool success = false;\n\n            // The element we are capturing to must be both enabled and visible.\n            if (element is UIElement)\n            {\n                UIElement e = element as UIElement;\n\n                if(e.IsVisible && e.IsEnabled)\n                {\n                    success = true;\n                }\n            }\n            else if (element is ContentElement)\n            {\n                ContentElement ce = element as ContentElement;\n","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs#L281-L317","documentation":"MouseDevice.Capture throws InvalidOperationException with SR.Invalid_IInputElement when the element passed for capture is a DependencyObject but not a valid IInputElement (i.e. not a UIElement, ContentElement, or UIElement3D). Mouse capture requires an element that participates in input. WPF validates the element type before capturing.","triggerScenarios":"Calling mouse.Capture(someVisualOrDependencyObject, mode) with an object like a plain Visual, DrawingVisual, or non-input DependencyObject that is not UIElement/ContentElement/UIElement3D.","commonSituations":"Capturing the mouse to a DrawingVisual or a derived Visual for custom rendering — a common need in custom-drawn controls that fails because Visual lacks input support.","solutions":["Capture to an enclosing UIElement (e.g. the control hosting the visual) instead of the raw Visual","Implement input participation by deriving from UIElement/UIElement3D/ContentElement","Check InputElement.IsValid(element) before calling Capture"],"exampleFix":"// before\nMouse.Capture(myDrawingVisual); // throws: not an IInputElement\n// after\nMouse.Capture(hostControl); // hostControl is the UIElement that renders the visual","handlingStrategy":"validation","validationCode":"if (element is DependencyObject && !InputElement.IsValid(element))\n    throw new InvalidOperationException(\"Capture target must be UIElement, ContentElement, or UIElement3D\");","typeGuard":"static bool CanReceiveMouseCapture(object o) =>\n    o is UIElement || o is ContentElement || o is UIElement3D;","tryCatchPattern":"try { Mouse.Capture(element, CaptureMode.Element); }\ncatch (InvalidOperationException) { Mouse.Capture(hostControl, CaptureMode.Element); }","preventionTips":["Only capture to UIElement/ContentElement/UIElement3D instances","For DrawingVisuals, capture the hosting control instead","Check InputElement.IsValid before capture calls"],"tags":["wpf","mouse","capture","type-mismatch","invalid-operation"],"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"}