dotnet/wpf · error · InvalidOperationException
SR.Format(SR.Invalid_IInputElement…
Error message
SR.Format(SR.Invalid_IInputElement, _stylusCapture.GetType())
What it means
WispLogic capture-apply path subscribes IsEnabled/IsVisible/IsHitTestVisible change handlers to the new _stylusCapture. When the capture target is neither UIElement, ContentElement, nor UIElement3D, no handler wiring is possible, so it throws InvalidOperationException naming the type. This mirrors error 697 but fires when establishing capture rather than releasing it.
Solutions
- Derive the capture target from UIElement (or ContentElement/UIElement3D)
- Capture the nearest supported ancestor instead of the visual itself
- Add a guard with InputElement.IsValid(element) before calling Capture
Example fix
// before
Stylus.Capture(drawingVisual, CaptureMode.Element);
// after
if (InputElement.IsValid(drawingVisual))
Stylus.Capture(drawingVisual, CaptureMode.Element);
else
Stylus.Capture(FindAncestor<UIElement>(drawingVisual), CaptureMode.Element); Defensive patterns
Strategy: type-guard
Validate before calling
if (InputElement.IsValid(element))
Stylus.Capture(element, CaptureMode.Element); Type guard
bool CanCapture(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;
Try / catch
try { Stylus.Capture(element, CaptureMode.Element); }
catch (InvalidOperationException ex) { Log.Warn("Invalid stylus capture element", ex); } Prevention
- Always derive capture targets from UIElement
- Wrap DrawingVisual content in a UIElement host before capture
- Use InputElement.IsValid as a pre-capture gate
When it happens
Trigger: Calling Stylus.Capture(element, ...) or StylusDevice.Capture with an element that is a DependencyObject but not one of the three supported input element types; the throw happens while wiring change notifications in WispLogic.
Common situations: Capturing a DrawingVisual or custom IInputElement; interop code capturing on non-element visuals; unit-test fakes passed to Stylus.Capture.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Format(SR.Invalid_IInputElement, oldCapture.GetType())
- SR.Format(SR.Invalid_IInputElement…
- SR.Format(SR.Invalid_IInputElement, oldOver.GetType())
- SR.Stylus_PenContextFailure
- captureMode
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a50b84a7e75f971c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs:2116
}
else if (o is ContentElement ce)
{
ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler;
// NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements.
//
// ce.IsVisibleChanged += _captureIsVisibleChangedEventHandler;
// ce.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler;
}
else if (o is UIElement3D uie3D)
{
uie3D.IsEnabledChanged += _captureIsEnabledChangedEventHandler;
uie3D.IsVisibleChanged += _captureIsVisibleChangedEventHandler;
uie3D.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler;
}
else
{
throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, _stylusCapture.GetType()));
}
}
// Oddly enough, update the IsStylusCaptureWithin property first. This is
// so any callbacks will see the more-common IsStylusCaptureWithin property
// set correctly.
UIElement.StylusCaptureWithinProperty.OnOriginValueChanged(oldCapture as DependencyObject, _stylusCapture as DependencyObject, ref _stylusCaptureWithinTreeState);
// Invalidate the IsStylusCaptured properties.
if (oldCapture != null)
{
o = oldCapture as DependencyObject;
o.SetValue(UIElement.IsStylusCapturedPropertyKey, false); // Same property for ContentElements
}
if (_stylusCapture != null)
{
o = _stylusCapture as DependencyObject;
o.SetValue(UIElement.IsStylusCapturedPropertyKey, true); // Same property for ContentElementsView on GitHub (pinned to 81131a70a4)