title: c# WPF - How to make commands that includes extra fields / properties
body: <p>So, I have this command call CloseCommand in my Commands folder. I also have a WindowViewModel class in my ViewModels folder. Here's the content inside the WindowViewModel:</p>
  
  <pre><code>using Test.Commands;
  
  namespace Test.ViewModels
  {
      public class WindowViewModel
      {
  
          #region Window
          public MainWindow mainWindow { get; set; }
          public CloseCommand CloseCommand{ get; set; } = new CloseCommand();
          #endregion
  
          public WindowViewModel(MainWindow mainWindow)
          {
              this.mainWindow = mainWindow;
              Test = "Hello";
              CloseCommand.mainWindow = this.mainWindow;
          }
          public WindowViewModel() { }
  
      }
  }
  </code></pre>
  
  <p>Here's the content inside the CloseCommand class:</p>
  
  <pre><code>using System;
  using System.Windows;
  using System.Windows.Input;
  
  namespace Test.Commands
  {
      public class CloseCommand: ICommand
      {
          public event EventHandler CanExecuteChanged;
          public MainWindow mainWindow { get; set; }
  
          public CloseCommand(MainWindow mainWindow)
          {
              this.mainWindow = mainWindow;
          }
  
          public CloseCommand() { }
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
          public void Execute(object parameter)
          {
              MessageBox.Show("Press OK to close");
              mainWindow.Close();
          }
      }
  }
  </code></pre>
  
  <p>Here's the content inside the MainWindow.xaml:</p>
  
  <pre><code>    &lt;Window.Resources&gt;
          &lt;!-- I have already defined "commands" when defining Window--&gt;
          &lt;commands:CloseCommand x:Key="CloseCommand"/&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Button Content="{Binding Test}" Background="Blue" Foreground="White" FontSize="50" Command="{StaticResource CloseCommand}"/&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>Now, when I run the program and press my button, it display the MessageBox I had defined in the <code>CloseCommand.Execute</code> method, but after I press the OK button in the <code>MessageBox</code>, it gives me an error: </p>
  
  <blockquote>
    <p>mainWindow property is null. </p>
  </blockquote>
  
  <p>I expect this problem happens because the <code>CloseCommand</code> is converted to an ICommand when I defined it in my <strong>MainWindow.xaml</strong> and in the Command property of the <code>Button</code>. So how do I fix this problem? Is my expectations right? Please correct me.</p>