dotnet/wpf · error · InvalidOperationException
SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())
Error message
SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())
What it means
WispStylusDevice.GetPosition (WispStylusDevice.cs:1162) throws InvalidOperationException(SR.Invalid_IInputElement) when the relativeTo argument is non-null and not a valid IInputElement (UIElement, ContentElement, or UIElement3D). GetPosition needs a supported input element to compute the stylus coordinate space.
Solutions
- Pass a UIElement, ContentElement, or UIElement3D as relativeTo (e.g. the hosting control instead of the raw visual).
- Validate InputElement.IsValid(relativeTo) before calling GetPosition.
- Pass null to get coordinates relative to the presentation source and convert manually if needed.
Example fix
// before
var pt = stylusDevice.GetPosition(myDrawingVisual); // throws
// after
if (InputElement.IsValid(myDrawingVisual)) { var pt = stylusDevice.GetPosition(myDrawingVisual); } Defensive patterns
Strategy: validation
Validate before calling
if (relativeTo != null && !InputElement.IsValid(relativeTo))
throw new ArgumentException("relativeTo must be UIElement, ContentElement, or UIElement3D"); Type guard
static bool IsValidPositionReference(IInputElement e) => e == null || e is UIElement || e is ContentElement || e is UIElement3D;
Try / catch
try { var pt = stylusDevice.GetPosition(relativeTo); } catch (InvalidOperationException ex) when (ex.Message.Contains("IInputElement")) { /* pass null and transform manually via the hosting UIElement */ } Prevention
- Use the hosting control (UIElement) as the coordinate reference, not raw visuals
- Validate relativeTo with InputElement.IsValid before calling
- Type reference parameters as UIElement to catch mistakes at compile time
When it happens
Trigger: Calling stylusDevice.GetPosition(relativeTo) with a raw Visual, Visual3D, or other DependencyObject that does not implement IInputElement properly.
Common situations: Passing a DrawingVisual or adorner-layer visual as the reference; variables widened to DependencyObject/Visual during refactoring; passing elements from third-party visual frameworks.
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…
- SR.Format(SR.Invalid_IInputElement, oldOver.GetType())
- SR.Format(SR.Invalid_IInputElement, _stylusOver.GetType())
- SR.Format(SR.Invalid_IInputElement, _stylusOver.GetType())
- 0x80040207
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c0433880f49800b1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs:1162
get
{
VerifyAccess();
return _stylusButtonCollection;
}
}
/////////////////////////////////////////////////////////////////////
/// <summary>
/// Calculates the position of the stylus relative to a particular element.
/// </summary>
internal override 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 != null)
{
relativePresentationSource = _inputSource;View on GitHub (pinned to 81131a70a4)