title: Understanding ICommand implementation without MVVM
body: <p>I am trying to understand how to work with Commands. 
  I read a lot about commands, and I know, that most times commands are used within the MVVM pattern. Also I know, that there is a RoutedCommand class - which is often used to save time while developing.</p>
  
  <p>But - I want to understand the basics - and exactly that's the problem. Here we go:</p>
  
  <p>In my app I defined a class 'MyCommand' :</p>
  
  <pre><code> public class MyCommand :ICommand
  {
      public void Execute(object parameter)
      {
          Console.WriteLine("Execute called!");
          CanExecuteChanged(null, null);
      }
  
      public bool CanExecute(object parameter)
      {
          Console.WriteLine("CanExecute called!");
          return true;
      }
  
      public event EventHandler CanExecuteChanged;
  }
  </code></pre>
  
  <p>Well - to get a static access I decided to make a class, just for all application commands:</p>
  
  <pre><code> public static class AppCommands
      {
          private static ICommand anyCommand = new MyCommand();
  
          public static ICommand AnyCommand
          {
              get { return anyCommand; }
          }
      }
  </code></pre>
  
  <p>Nearly there. Now I put two buttons in my MainWindow. One of them is 'binded' to the command:</p>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" /&gt;
      &lt;Button Content="Button" Height="23" Name="button2" Width="75" Command="{x:Static local:AppCommands.AnyCommand}" CommandParameter="Hello"/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>And here is the MainWindow.cs :</p>
  
  <pre><code>public MainWindow()
  
  
    {
          InitializeComponent();
          AppCommands.AnyCommand.CanExecuteChanged += MyEventHandler;
      }
  
  
      private void button1_Click(object sender, RoutedEventArgs e)
      {
         // Nothing for the moment
      }
  
      private void MyEventHandler(object sender, EventArgs e)
      {
          Console.WriteLine("MyEventHandler called");
      }
  </code></pre>
  
  <p>So - let's run my project. As you can see, I made some console outputs.
  If I click on button2, the output is:</p>
  
  <p>CanExecute called!
  Execute called!
  CanExecute called!
  MyEventHandler called</p>
  
  <p>So, in my eyes this is what happens:
  1.) the command on the button is 'activated'. To check if the execute method should be called, the CanExecute method is called.
  2.) If the CanExecute method returns true, then the Execute method is called.
  3.) Within the execute method, I defined, that the event 'CanExecuteChanged' should be raised. Calling it will 1st check 'CanExecute' and then call the eventhandler.</p>
  
  <p>This is not clear to me. The only way to call the event is within the Execute method. But the Execute method is called via Command logic after checking CanExecute. Calling the event also checks CanExecute, but why? I am confused.</p>
  
  <p>And things are getting more confused, when I try to disable the button.
  Lets say, there is a 'CommandParameter' - and 'CanExecute' now works with it. So it could be, that the method returns false. In this case the button is disabled - okay.</p>
  
  <p>But: how do I reactivate it? As we already know: I can only raise the CanExecuteChange event from within my command class. So - as we are not able to click the disabled button - the command won't call CanExecute (or even Execute) method.</p>
  
  <p>Seems to me there is something important I can't see - but I really can't find it.</p>
  
  <p>Could you please help me?</p>