dotnet/wpf · error · InvalidOperationException

SR.Format(SR.Invalid_IInputElement…

Error message

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

What it means

In the same UpdateStylusCapture flow, when subscribing capture-change handlers on the NEW capture element (_stylusCapture), the code throws InvalidOperationException (SR.Invalid_IInputElement with _stylusCapture.GetType()) if the element is not UIElement, ContentElement, or UIElement3D.

Solutions

  1. Ensure the captured element derives from UIElement/ContentElement/UIElement3D
  2. Type-check before capture and reject unsupported elements at your API boundary
  3. Wrap Stylus.Capture in a helper that validates the element type

Example fix

// before
Stylus.Capture(externalElement, CaptureMode.SubElement);
// after
var dep = externalElement as DependencyObject;
if (externalElement is UIElement || externalElement is ContentElement || externalElement is UIElement3D)
{
    Stylus.Capture(externalElement, CaptureMode.SubElement);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(element is UIElement || element is ContentElement || element is UIElement3D)) throw new NotSupportedException("Element type not supported for stylus capture");

Type guard

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

Try / catch

try { Stylus.Capture(element, CaptureMode.Element); }
catch (InvalidOperationException) { /* fall back to mouse capture or reject */ }

Prevention

When it happens

Trigger: Stylus capture being set (Stylus.Capture / Capture) to an unsupported IInputElement type, triggering handler registration on the new capture element.

Common situations: Same as the release-side failure: custom IInputElement implementations or foreign framework elements used with stylus capture, often only surfacing later in the pointer logic.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

                    {
                        element.IsEnabledChanged += _captureIsEnabledChangedEventHandler;
                        element.IsVisibleChanged += _captureIsVisibleChangedEventHandler;
                        element.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler;
                    }
                    else if (o is ContentElement ce)
                    {
                        // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements.
                        ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler;
                    }
                    else if (o is UIElement3D element3D)
                    {
                        element3D.IsEnabledChanged += _captureIsEnabledChangedEventHandler;
                        element3D.IsVisibleChanged += _captureIsVisibleChangedEventHandler;
                        element3D.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;

View on GitHub (pinned to 81131a70a4)