dotnet/wpf · error · InvalidOperationException
SR.Format(SR.Invalid_IInputElement…
Error message
SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType())
What it means
WispStylusDevice.Capture (WispStylusDevice.cs:245) throws InvalidOperationException(SR.Invalid_IInputElement) when the element passed as the stylus capture target is a DependencyObject that is not a valid IInputElement (not UIElement, ContentElement, or UIElement3D). The stylus capture API only supports those three element classes.
Solutions
- Cast to / use the concrete UIElement, ContentElement, or UIElement3D instance when capturing.
- Check InputElement.IsValid(element) before calling Capture.
- Capture an ancestor that is a proper input element instead of the raw visual.
Example fix
// before DependencyObject target = myVisual; stylusDevice.Capture(target, CaptureMode.Element); // throws // after if (InputElement.IsValid(myVisual)) stylusDevice.Capture(myVisual, CaptureMode.Element);
Defensive patterns
Strategy: type-guard
Validate before calling
if (element != null && !InputElement.IsValid(element)) return; // abort capture on unsupported element
Type guard
static bool IsCapturable(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;
Try / catch
try { stylusDevice.Capture(el, mode); } catch (InvalidOperationException ex) when (ex.Message.Contains("IInputElement")) { /* find nearest supported ancestor and capture that */ } Prevention
- Type capture-target variables as UIElement rather than DependencyObject/Visual
- Check InputElement.IsValid before any capture call
- Capture the control, not its raw visual children
When it happens
Trigger: Calling stylusDevice.Capture(el, mode) with el being a Visual/DependencyObject (e.g. a raw Visual, Visual3D, or custom DependencyObject) rather than a supported IInputElement.
Common situations: Grabbing capture on drawing visuals or decorator children held as DependencyObject references; refactored code that widened a variable's type from UIElement to DependencyObject.
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
- captureMode
- SR.Format(SR.Invalid_IInputElement…
- SR.Format(SR.Invalid_IInputElement, oldCapture.GetType())
- SR.Format(SR.Invalid_IInputElement, oldOver.GetType())
- SR.Format(SR.Invalid_IInputElement, relativeTo.GetType())
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/640b7a49dfb77fd8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs:245
{
throw new System.ComponentModel.InvalidEnumArgumentException("captureMode", (int)captureMode, typeof(CaptureMode));
}
if (element == null)
{
captureMode = CaptureMode.None;
}
if (captureMode == CaptureMode.None)
{
element = null;
}
// Validate that element is either a UIElement, a ContentElement or a UIElement3D.
DependencyObject doStylusCapture = element as DependencyObject;
if (doStylusCapture != null && !InputElement.IsValid(element))
{
throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType()));
}
doStylusCapture?.VerifyAccess();
bool success = false;
// The element we are capturing to must be both enabled and visible.
UIElement e = element as UIElement;
if (e != null)
{
if (e.IsVisible || e.IsEnabled)
{
success = true;
}
}
else
{View on GitHub (pinned to 81131a70a4)