dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

WispLogic.ReevaluateStylusCapture (capture-release path) unsubscribes IsEnabled/IsVisible/IsHitTestVisible change handlers from the old capture element. If oldCapture is not UIElement, ContentElement, or UIElement3D, it throws InvalidOperationException naming its type, since handler wiring is only defined for those element kinds.

Solutions

  1. Always capture on UIElement/ContentElement/UIElement3D objects
  2. Inspect Stylus.Capture(...) call sites and remove capture of custom non-element objects
  3. If state is already corrupted, avoid capturing unsupported elements so the stale value never enters the capture path

Example fix

// before
Stylus.Capture(myCustomElement); // implements IInputElement only
// after
Stylus.Capture(myUIElement); // UIElement-based capture target
Defensive patterns

Strategy: type-guard

Validate before calling

if (oldCapture is UIElement || oldCapture is ContentElement || oldCapture is UIElement3D) { /* safe to change capture */ }

Type guard

bool IsSupportedCaptureElement(IInputElement e) => e is UIElement || e is ContentElement || e is UIElement3D;

Try / catch

try { Stylus.Capture(newElement); }
catch (InvalidOperationException ex) { Log.Warn("Capture state contained unsupported element", ex); }

Prevention

When it happens

Trigger: Setting/changing Stylus.Capture where the previously captured object is a DependencyObject of an unsupported type; thrown synchronously when requery/capture changes (e.g. in ChangeStylusCaptureState).

Common situations: Custom IInputElement implementations used as capture targets; capture state corrupted by earlier invalid capture on a non-element; migrated code from mouse capture semantics.

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/9dcd59e0701d49b0. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs:2087

                    }
                    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, oldCapture.GetType())); 
                    }
                }
                if (_stylusCapture != null)
                {
                    o = _stylusCapture as DependencyObject;
                    if (o is UIElement uie)
                    {
                        uie.IsEnabledChanged += _captureIsEnabledChangedEventHandler;
                        uie.IsVisibleChanged += _captureIsVisibleChangedEventHandler;
                        uie.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler;
                    }
                    else if (o is ContentElement ce)
                    {
                        ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler;

                        // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements.
                        //
                        // ce.IsVisibleChanged += _captureIsVisibleChangedEventHandler;

View on GitHub (pinned to 81131a70a4)