{"record":{"id":"6bb0e850efa4a83b","repo":"stride3d/stride","slug":"the-parameter-of-this-command-must-be-an-instance-of-window","errorCode":null,"errorMessage":"The parameter of this command must be an instance of 'Window'.","messagePattern":"The parameter of this command must be an instance of 'Window'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Commands/SystemCommand.cs","lineNumber":32,"sourceCode":"\n        internal SystemCommand([NotNull] Func<Window, bool> canExecute, [NotNull] Action<Window> execute)\n        {\n            if (canExecute == null) throw new ArgumentNullException(nameof(canExecute));\n            if (execute == null) throw new ArgumentNullException(nameof(execute));\n            this.canExecute = canExecute;\n            this.execute = execute;\n        }\n\n        public bool CanExecute(object parameter)\n        {\n            var window = parameter as Window;\n            return window != null && canExecute(window);\n        }\n\n        public void Execute([NotNull] object parameter)\n        {\n            var window = parameter as Window;\n            if (window == null) throw new ArgumentException(\"The parameter of this command must be an instance of \\'Window\\'.\");\n            execute(window);\n        }\n\n        // We provide an empty `add' and `remove' to avoid a warning about unused events that we have\n        // to implement as they are part of the ICommand definition.\n        public event EventHandler CanExecuteChanged { add { } remove { } }\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":41,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Commands/SystemCommand.cs#L14-L41","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Set CommandParameter=\"{Binding RelativeSource={RelativeSource AncestorType=Window}}\" in XAML so the hosting Window is passed.","In code, call command.Execute(window) with the actual Window instance.","Add a null/typeof check before invoking if the parameter source is dynamic."],"exampleFix":"<!-- before -->\n<Button Command=\"{sys:SystemCommand CloseCommand}\"/>\n<!-- after -->\n<Button Command=\"{sys:SystemCommand CloseCommand}\"\n         CommandParameter=\"{Binding RelativeSource={RelativeSource AncestorType=Window}}\"/>","handlingStrategy":"type-guard","validationCode":"if (parameter is Window w) { command.Execute(w); } else { log.Warn(\"SystemCommand requires a Window parameter\"); }","typeGuard":"bool TryGetWindow(object parameter, out Window window) { window = parameter as Window; return window != null; }","tryCatchPattern":"try { command.Execute(param); }\ncatch (ArgumentException ex) { log.Error(\"SystemCommand parameter was not a Window\", ex); }","preventionTips":["Always bind CommandParameter to the ancestor Window in XAML.","Check CanExecute(parameter) — it already returns false for non-Window — before Execute.","Never call ICommand.Execute from view models with view-agnostic parameters for SystemCommand."],"tags":["wpf","icommand","argument-validation"],"backgroundTag":"type-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}