dotnet/wpf · error · InvalidOperationException

SR.Format(SR.Invalid_IInputElement…

Error message

SR.Format(SR.Invalid_IInputElement, doStylusCapture.GetType())

What it means

PointerStylusDevice.Capture requires the element to be a valid input element: a UIElement, ContentElement, or UIElement3D (checked via InputElement.IsValid). If the element is a DependencyObject of any other type, Capture throws InvalidOperationException naming its type. Stylus capture tracking needs the property-change hooks only those element types provide.

Solutions

  1. Capture on the nearest ancestor UIElement instead of the raw visual
  2. Derive the capture target from UIElement (or ContentElement/UIElement3D)
  3. Use VisualTreeHelper to walk up to a supported input element before capturing

Example fix

// before
dev.Capture(drawingVisual, CaptureMode.Element); // DrawingVisual is not IInputElement
// after
var host = FindAncestor<UIElement>(drawingVisual);
if (host != null) dev.Capture(host, CaptureMode.Element);
Defensive patterns

Strategy: type-guard

Validate before calling

if (element == null || InputElement.IsValid(element))
    dev.Capture(element, mode);

Type guard

bool IsValidInputElement(object e) => e == null || InputElement.IsValid(e);

Try / catch

try { dev.Capture(element, mode); }
catch (InvalidOperationException ex) { Log.Warn("Capture target not an IInputElement", ex); }

Prevention

When it happens

Trigger: Calling StylusDevice.Capture(otherElement, mode) where otherElement is a DependencyObject (e.g. a Visual, a Freezable, or a custom IInputElement) but not UIElement/ContentElement/UIElement3D.

Common situations: Capturing on a DrawingVisual (visual but not an input element); capturing on a Grid3D child or non-element node; refactoring code that used mouse capture on visuals into 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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/cdbc4de0cc2730bb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerStylusDevice.cs:552

                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?.IsVisible ?? false) || (e?.IsEnabled ?? false))
            {
                success = true;
            }
            else
            {
                ContentElement ce = element as ContentElement;

                if (ce?.IsEnabled ?? false)

View on GitHub (pinned to 81131a70a4)