{"record":{"id":"1b9f975f21c93f36","repo":"dotnet/wpf","slug":"sr-invalid-iinputelement-target-gettype","errorCode":null,"errorMessage":"SR.Invalid_IInputElement (target.GetType())","messagePattern":"SR\\.Invalid_IInputElement \\(target\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs","lineNumber":122,"sourceCode":"            add { CommandManager.RequerySuggested += value; }\n            remove { CommandManager.RequerySuggested -= value; }\n        }\n\n        #endregion\n\n        #region Public Methods\n\n        /// <summary>\n        ///     Executes the command with the given parameter on the given target.\n        /// </summary>\n        /// <param name=\"parameter\">Parameter to be passed to any command handlers.</param>\n        /// <param name=\"target\">Element at which to begin looking for command handlers.</param>\n        public void Execute(object parameter, IInputElement target)\n        {\n            // We only support UIElement, ContentElement and UIElement3D\n            if ((target != null) && !InputElement.IsValid(target))\n            {\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, target.GetType()));\n            }\n\n            if (target == null)\n            {\n                target = FilterInputElement(Keyboard.FocusedElement);\n            }\n\n            ExecuteImpl(parameter, target, false);\n        }\n\n        /// <summary>\n        ///     Whether the command can be executed with the given parameter on the given target.\n        /// </summary>\n        /// <param name=\"parameter\">Parameter to be passed to any command handlers.</param>\n        /// <param name=\"target\">The target element on which to begin looking for command handlers.</param>\n        /// <returns>true if the command can be executed, false otherwise.</returns>\n        public bool CanExecute(object parameter, IInputElement target)\n        {","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs#L104-L140","documentation":"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.","triggerScenarios":"command.Execute(parameter, someCustomIInputElement) where the object implements IInputElement but derives from neither UIElement, ContentElement nor UIElement3D.","commonSituations":"Custom controls implementing IInputElement directly, unit-test stubs/mocks of IInputElement passed as targets, or passing null-derived non-visual adapters.","solutions":["Pass a UIElement/ContentElement/UIElement3D instance (e.g. the Window or a control) as target instead of a custom IInputElement.","Pass null to let the command route to Keyboard.FocusedElement.","Rework custom elements to derive from FrameworkElement (a UIElement) rather than implementing IInputElement directly.","In tests, use a real FrameworkElement instance rather than a mock IInputElement."],"exampleFix":"// before\nmyCommand.Execute(param, myCustomInputElement);\n// after\nmyCommand.Execute(param, this); // 'this' is a UIElement, or null for focused element","handlingStrategy":"type-guard","validationCode":"if (target is UIElement || target is ContentElement || target is UIElement3D)\n    command.Execute(param, target);\nelse\n    command.Execute(param, null);","typeGuard":"static bool IsValidCommandTarget(IInputElement t) => t is UIElement or ContentElement or UIElement3D;","tryCatchPattern":"try { command.Execute(param, target); } catch (InvalidOperationException ex) when (ex.Message.Contains(target.GetType().Name)) { command.Execute(param, null); }","preventionTips":["Only pass UIElement/ContentElement/UIElement3D as command targets","Pass null to target the keyboard-focused element","Avoid mocking IInputElement in command tests; use a real FrameworkElement"],"tags":["wpf","routed-command","invalid-operation"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}