stride3d/stride · error · ArgumentException

The parameter of this command must be an instance of…

Error message

The parameter of this command must be an instance of 'Window'.

What it means

SystemCommand is an ICommand whose Execute casts its parameter to Window and invokes the stored action with it. If the bound parameter is not a Window instance, an ArgumentException is thrown. This guards commands like Close/Minimize/Maximize that fundamentally need the target window.

Solutions

  1. Set CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}" in XAML so the hosting Window is passed.
  2. In code, call command.Execute(window) with the actual Window instance.
  3. Add a null/typeof check before invoking if the parameter source is dynamic.

Example fix

<!-- before -->
<Button Command="{sys:SystemCommand CloseCommand}"/>
<!-- after -->
<Button Command="{sys:SystemCommand CloseCommand}"
         CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"/>
Defensive patterns

Strategy: type-guard

Validate before calling

if (parameter is Window w) { command.Execute(w); } else { log.Warn("SystemCommand requires a Window parameter"); }

Type guard

bool TryGetWindow(object parameter, out Window window) { window = parameter as Window; return window != null; }

Try / catch

try { command.Execute(param); }
catch (ArgumentException ex) { log.Error("SystemCommand parameter was not a Window", ex); }

Prevention

When it happens

Trigger: Invoking Execute (directly or via a CommandBinding/button) on a SystemCommand whose CommandParameter is null, missing, or bound to something other than a Window (e.g. a ViewModel, string, or control).

Common situations: XAML binding mistake where CommandParameter is omitted or bound to the wrong DataContext property; wiring the command in code with a non-window parameter; calling command.Execute(...) from a view model.

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 stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/6bb0e850efa4a83b. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Commands/SystemCommand.cs:32

        internal SystemCommand([NotNull] Func<Window, bool> canExecute, [NotNull] Action<Window> execute)
        {
            if (canExecute == null) throw new ArgumentNullException(nameof(canExecute));
            if (execute == null) throw new ArgumentNullException(nameof(execute));
            this.canExecute = canExecute;
            this.execute = execute;
        }

        public bool CanExecute(object parameter)
        {
            var window = parameter as Window;
            return window != null && canExecute(window);
        }

        public void Execute([NotNull] object parameter)
        {
            var window = parameter as Window;
            if (window == null) throw new ArgumentException("The parameter of this command must be an instance of \'Window\'.");
            execute(window);
        }

        // We provide an empty `add' and `remove' to avoid a warning about unused events that we have
        // to implement as they are part of the ICommand definition.
        public event EventHandler CanExecuteChanged { add { } remove { } }
    }
}

View on GitHub (pinned to 96fad776d2)