dotnet/wpf · error · InvalidOperationException

SR.Invalid_IInputElement (target.GetType())

Error message

SR.Invalid_IInputElement (target.GetType())

What it means

RoutedCommand.Execute requires the target to be UIElement, ContentElement or UIElement3D; passing another IInputElement implementation throws InvalidOperationException (SR.Invalid_IInputElement) with the actual type. WPF's command routing only works with these three element bases.

Solutions

  1. Pass a UIElement/ContentElement/UIElement3D instance (e.g. the Window or a control) as target instead of a custom IInputElement.
  2. Pass null to let the command route to Keyboard.FocusedElement.
  3. Rework custom elements to derive from FrameworkElement (a UIElement) rather than implementing IInputElement directly.
  4. In tests, use a real FrameworkElement instance rather than a mock IInputElement.

Example fix

// before
myCommand.Execute(param, myCustomInputElement);
// after
myCommand.Execute(param, this); // 'this' is a UIElement, or null for focused element
Defensive patterns

Strategy: type-guard

Validate before calling

if (target is UIElement || target is ContentElement || target is UIElement3D)
    command.Execute(param, target);
else
    command.Execute(param, null);

Type guard

static bool IsValidCommandTarget(IInputElement t) => t is UIElement or ContentElement or UIElement3D;

Try / catch

try { command.Execute(param, target); } catch (InvalidOperationException ex) when (ex.Message.Contains(target.GetType().Name)) { command.Execute(param, null); }

Prevention

When it happens

Trigger: command.Execute(parameter, someCustomIInputElement) where the object implements IInputElement but derives from neither UIElement, ContentElement nor UIElement3D.

Common situations: Custom controls implementing IInputElement directly, unit-test stubs/mocks of IInputElement passed as targets, or passing null-derived non-visual adapters.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs:122

            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        ///     Executes the command with the given parameter on the given target.
        /// </summary>
        /// <param name="parameter">Parameter to be passed to any command handlers.</param>
        /// <param name="target">Element at which to begin looking for command handlers.</param>
        public void Execute(object parameter, IInputElement target)
        {
            // We only support UIElement, ContentElement and UIElement3D
            if ((target != null) && !InputElement.IsValid(target))
            {
                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, target.GetType()));
            }

            if (target == null)
            {
                target = FilterInputElement(Keyboard.FocusedElement);
            }

            ExecuteImpl(parameter, target, false);
        }

        /// <summary>
        ///     Whether the command can be executed with the given parameter on the given target.
        /// </summary>
        /// <param name="parameter">Parameter to be passed to any command handlers.</param>
        /// <param name="target">The target element on which to begin looking for command handlers.</param>
        /// <returns>true if the command can be executed, false otherwise.</returns>
        public bool CanExecute(object parameter, IInputElement target)
        {

View on GitHub (pinned to 81131a70a4)