dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

MouseDevice.Capture throws InvalidOperationException with SR.Invalid_IInputElement when the element passed for capture is a DependencyObject but not a valid IInputElement (i.e. not a UIElement, ContentElement, or UIElement3D). Mouse capture requires an element that participates in input. WPF validates the element type before capturing.

Solutions

  1. Capture to an enclosing UIElement (e.g. the control hosting the visual) instead of the raw Visual
  2. Implement input participation by deriving from UIElement/UIElement3D/ContentElement
  3. Check InputElement.IsValid(element) before calling Capture

Example fix

// before
Mouse.Capture(myDrawingVisual); // throws: not an IInputElement
// after
Mouse.Capture(hostControl); // hostControl is the UIElement that renders the visual
Defensive patterns

Strategy: validation

Validate before calling

if (element is DependencyObject && !InputElement.IsValid(element))
    throw new InvalidOperationException("Capture target must be UIElement, ContentElement, or UIElement3D");

Type guard

static bool CanReceiveMouseCapture(object o) =>
    o is UIElement || o is ContentElement || o is UIElement3D;

Try / catch

try { Mouse.Capture(element, CaptureMode.Element); }
catch (InvalidOperationException) { Mouse.Capture(hostControl, CaptureMode.Element); }

Prevention

When it happens

Trigger: Calling mouse.Capture(someVisualOrDependencyObject, mode) with an object like a plain Visual, DrawingVisual, or non-input DependencyObject that is not UIElement/ContentElement/UIElement3D.

Common situations: Capturing the mouse to a DrawingVisual or a derived Visual for custom rendering — a common need in custom-drawn controls that fails because Visual lacks input support.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs:299

            {
                throw new System.ComponentModel.InvalidEnumArgumentException("captureMode", (int)captureMode, typeof(CaptureMode));
            }

            if (element == null)
            {
                captureMode = CaptureMode.None;
            }

            if (captureMode == CaptureMode.None)
            {
                element = null;
            }

            // Validate that elt is either a UIElement, a ContentElement or a UIElement3D.
            DependencyObject eltDO = element as DependencyObject;
            if (eltDO != null && !InputElement.IsValid(element))
            {
                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, eltDO.GetType()));
            }

            bool success = false;

            // The element we are capturing to must be both enabled and visible.
            if (element is UIElement)
            {
                UIElement e = element as UIElement;

                if(e.IsVisible && e.IsEnabled)
                {
                    success = true;
                }
            }
            else if (element is ContentElement)
            {
                ContentElement ce = element as ContentElement;

View on GitHub (pinned to 81131a70a4)