{"record":{"id":"ff1a5cc7f2ad35b4","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-relativeto-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, relativeTo\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs","lineNumber":546,"sourceCode":"        {\n            get\n            {\n                return GetButtonState(MouseButton.XButton2);\n            }\n        }\n\n        /// <summary>\n        ///     Calculates the position of the mouse relative to\n        ///     a particular element.\n        /// </summary>\n        public Point GetPosition(IInputElement relativeTo)\n        {\n//             VerifyAccess();\n\n            // Validate that relativeTo is either a UIElement, a ContentElement or a UIElement3D.\n            if (relativeTo != null && !InputElement.IsValid(relativeTo))\n            {\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, relativeTo.GetType()));\n            }\n\n            PresentationSource relativePresentationSource = null;\n\n            if (relativeTo != null)\n            {\n                DependencyObject dependencyObject = relativeTo as  DependencyObject;\n                DependencyObject containingVisual = InputElement.GetContainingVisual(dependencyObject);\n\n                if (containingVisual != null)\n                {\n                    relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual);\n                }\n            }\n            else\n            {\n                if (_inputSource is not null)\n                {","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs#L528-L564","documentation":"MouseDevice.GetPosition throws InvalidOperationException with SR.Invalid_IInputElement when the relativeTo argument is non-null but is not a valid input element (UIElement, ContentElement, or UIElement3D). The mouse position must be reported relative to an element that can receive input. WPF validates the reference element's type before computing coordinates.","triggerScenarios":"Calling Mouse.GetPosition(someVisual) or mouse.GetPosition(relativeTo) with a plain Visual/DrawingVisual/DependencyObject that does not implement input properly.","commonSituations":"Requesting mouse coordinates relative to a DrawingVisual in a custom control; passing a Visual returned from VisualTreeHelper that is not a UIElement.","solutions":["Call Mouse.GetPosition on the hosting UIElement or the Window instead of the raw Visual","Convert coordinates afterwards with TransformToVisual if you need them relative to a non-input Visual","Guard with InputElement.IsValid(relativeTo) before calling"],"exampleFix":"// before\nPoint p = Mouse.GetPosition(myDrawingVisual); // throws\n// after\nPoint pH = Mouse.GetPosition(hostControl);          // hostControl is UIElement\nPoint p = hostControl.TransformToVisual(myDrawingVisual).Transform(pH);","handlingStrategy":"validation","validationCode":"if (relativeTo != null && !InputElement.IsValid(relativeTo))\n    throw new InvalidOperationException(\"relativeTo must be UIElement, ContentElement, or UIElement3D\");","typeGuard":"static bool IsValidMouseRelative(object o) =>\n    o == null || o is UIElement || o is ContentElement || o is UIElement3D;","tryCatchPattern":"try { var p = Mouse.GetPosition(relativeTo); }\ncatch (InvalidOperationException) { var p = Mouse.GetPosition((UIElement)hostControl); }","preventionTips":["Pass the hosting UIElement or Window to GetPosition, not raw Visuals","Use TransformToVisual to convert to non-input Visual coordinates","Verify VisualTreeHelper results are UIElements before using them as coordinate references"],"tags":["wpf","mouse","coordinates","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"}