title: disable a WPF button before a process and enable after the process completes in WPF MVVM
body: <p>I have a button that does some processing and I need to disable it before the process starts and enable it after the process completes. I need to accomplish this in mvvm pattern.</p>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="ButtonCommandBindingMVVM.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:ButtonCommandBindingMVVM"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800"&gt;
  &lt;Window.Resources&gt;
      &lt;local:ViewModel x:Key="vm"/&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="40"/&gt;
          &lt;RowDefinition Height="40"/&gt;
          &lt;RowDefinition Height="40"/&gt;
          &lt;RowDefinition Height="*"/&gt;
          &lt;RowDefinition Height="40"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Button Grid.Row="1"
              x:Name="Button1"
              Width="100"
              Height="27"
              Content="Say Hello"
              Command="{Binding Button1_ClickCommand, Source={StaticResource vm}}"
              /&gt;
      &lt;Button Grid.Row="2"
              x:Name="Button2"
              Width="100"
              Height="27"
              Content="Say Welcome"
              Command="{Binding Button2_ClickCommand, Source={StaticResource vm}}"
              /&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Command.cs</strong>
  This is a relay command class.</p>
  
  <pre><code>class Command : ICommand
  {
      Action&lt;object&gt; ExecuteMethod;
  
      Func&lt;object, bool&gt; CanExecuteMethod;
  
      public Command(Action&lt;object&gt; ExecuteMethod, Func&lt;object, bool&gt; CanExecuteMethod)
      {
          this.ExecuteMethod = ExecuteMethod;
          this.CanExecuteMethod = CanExecuteMethod;
      }
  
      public bool CanExecute(object parameter)
      {
          return true;
      }
  
      public void Execute(object parameter)
      {
          ExecuteMethod(parameter);
      }
  
      public event EventHandler CanExecuteChanged;
  
  }
  </code></pre>
  
  <p>ViewModel.cs</p>
  
  <pre><code>public class ViewModel
  {
      public ICommand Button1_ClickCommand { get; set; }
      public ICommand Button2_ClickCommand { get; set; }
  
      public ViewModel()
      {
          Button1_ClickCommand = new Command(ExecuteMethodButton1_ClickCommand, CanExecuteMethodButton1_ClickCommand);
          Button2_ClickCommand = new Command(ExecuteMethodButton2_ClickCommand, CanExecuteMethodButton2_ClickCommand);
      }
  
      private bool CanExecuteMethodButton1_ClickCommand(object parameter)
      {
          return true;
      }
  
      private void ExecuteMethodButton1_ClickCommand(object parameter)
      {
          await Task.Run(() =&gt;
          {
              Thread.Sleep(5000);
          });
          MessageBox.Show("Hello");
      }
  
      private bool CanExecuteMethodButton2_ClickCommand(object parameter)
      {
          return true;
      }
  
      private void ExecuteMethodButton2_ClickCommand(object parameter)
      {
          MessageBox.Show("Welcome");
      }
  }
  </code></pre>
  
  <p>Disable the Button1 and enable it after the thread sleeps for 5 secs.</p>