dotnet/wpf · error · InvalidOperationException
SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())
Error message
SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())
What it means
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.
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
Example fix
// before Point p = Mouse.GetPosition(myDrawingVisual); // throws // after Point pH = Mouse.GetPosition(hostControl); // hostControl is UIElement Point p = hostControl.TransformToVisual(myDrawingVisual).Transform(pH);
Defensive patterns
Strategy: validation
Validate before calling
if (relativeTo != null && !InputElement.IsValid(relativeTo))
throw new InvalidOperationException("relativeTo must be UIElement, ContentElement, or UIElement3D"); Type guard
static bool IsValidMouseRelative(object o) =>
o == null || o is UIElement || o is ContentElement || o is UIElement3D; Try / catch
try { var p = Mouse.GetPosition(relativeTo); }
catch (InvalidOperationException) { var p = Mouse.GetPosition((UIElement)hostControl); } Prevention
- 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
When it happens
Trigger: Calling Mouse.GetPosition(someVisual) or mouse.GetPosition(relativeTo) with a plain Visual/DrawingVisual/DependencyObject that does not implement input properly.
Common situations: Requesting mouse coordinates relative to a DrawingVisual in a custom control; passing a Visual returned from VisualTreeHelper that is not a UIElement.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- SR.Format(SR.Invalid_IInputElement, eltDO.GetType())
- SR.ListView_IllegalChildrenType
- ' ' is not a Visual or Visual3D.
- 0x80040207
- 0x80070057
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ff1a5cc7f2ad35b4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs:546
{
get
{
return GetButtonState(MouseButton.XButton2);
}
}
/// <summary>
/// Calculates the position of the mouse relative to
/// a particular element.
/// </summary>
public Point GetPosition(IInputElement relativeTo)
{
// VerifyAccess();
// Validate that relativeTo is either a UIElement, a ContentElement or a UIElement3D.
if (relativeTo != null && !InputElement.IsValid(relativeTo))
{
throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, relativeTo.GetType()));
}
PresentationSource relativePresentationSource = null;
if (relativeTo != null)
{
DependencyObject dependencyObject = relativeTo as DependencyObject;
DependencyObject containingVisual = InputElement.GetContainingVisual(dependencyObject);
if (containingVisual != null)
{
relativePresentationSource = PresentationSource.CriticalFromVisual(containingVisual);
}
}
else
{
if (_inputSource is not null)
{View on GitHub (pinned to 81131a70a4)