title: relaycommand pass parameter wpf
body: <p>All I want to do is pass a parameter from xaml into a viewmodel. The CanExecuteChanged event is not firing for my command in order to enable a button which performs an operation.</p>
  
  <p>I have no problem executing this logic if no parameter is passed into the viewmodel.</p>
  
  <p>I think something needs to be changed in my Relaycommand class. Can somebody please help me configure this properly? I have viewed the answers to this type of question, but still am having trouble getting the Delete button enabled in order to execute the DeleteThanks method.</p>
  
  <p>In the ICommand.CanExecute method of the RelayCommand class, the _TargetCanExecuteMethod &amp; _TargetExecuteMethod are always null; therefore, not executing the CanDeleteThanks method in the viewmodel.</p>
  
  <p>Note that the second RelayCommand method in the same named class needed to be inserted there due to the signature of the Delete command. However, those objects inside that method are not implemented in the ICommand.CanExecute method.</p>
  
  <p>Here is my xaml:</p>
  
  <pre><code>&lt;Button x:Name="btnDelete"
                      Content="Delete"
                      Command="{Binding DeleteThanksCommand}"
                      CommandParameter="{Binding Text, ElementName=Subject}"
                      IsEnabled="{Binding DeleteEnabled}"
                      HorizontalAlignment="Left"
                      Grid.Row="0"
                      Grid.Column="0" Foreground="#FF0C1334"&gt;
              &lt;/Button&gt;
  </code></pre>
  
  <p>Here is my viewmodel:</p>
  
  <pre><code>public GiveThanksViewModel()
          {
              DeleteThanksCommand = new RelayCommand(param =&gt; DeleteThanks(param), param =&gt; CanDeleteThanks(param));
          }
  
  private bool _DeleteEnabled;
          public bool DeleteEnabled
          {
              get
              {
                  return _DeleteEnabled;
              }
              set
              {
                  if (_DeleteEnabled != value)
                  {
                      _DeleteEnabled = value;
                  }
              }
          }
  
  public RelayCommand DeleteThanksCommand { get; private set; }
  
  private void DeleteThanks(object action)
          {
              try
              {
                  ...
                      DeleteEnabled = false;
                      DeleteThanksCommand.RaiseCanExecuteChanged();
                  }
              }
              catch (Exception ex)
              {
                  messageService.ShowNotification(ex.Message);
              }
          }
  
          private bool CanDeleteThanks(object parameter)
          {
              return DeleteEnabled;
          }
  </code></pre>
  
  <p>Here is the Relaycommand class:</p>
  
  <pre><code>public class RelayCommand : ICommand
      {
          Action _TargetExecuteMethod;
          Func&lt;bool&gt; _TargetCanExecuteMethod;
          private Action&lt;object&gt; action;
          private Func&lt;object, bool&gt; canDeleteThanks;
  
          public RelayCommand(Action executeMethod, Func&lt;bool&gt; canExecuteMethod)
          {
              _TargetExecuteMethod = executeMethod;
              _TargetCanExecuteMethod = canExecuteMethod;
          }
  
          public RelayCommand(Action&lt;object&gt; action, Func&lt;object, bool&gt; canDeleteThanks)
          {
              this.action = action;
              this.canDeleteThanks = canDeleteThanks;
          }
  
          public void RaiseCanExecuteChanged()
          {
              CanExecuteChanged(this, EventArgs.Empty);
          }
          #region ICommand Members
  
          bool ICommand.CanExecute(object parameter)
          {
              if (_TargetCanExecuteMethod != null)
              {
                  return _TargetCanExecuteMethod();
              }
              if (_TargetExecuteMethod != null)
              {
                  return true;
              }
              return false;
          }
  
          // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command
          // Prism commands solve this in their implementation
          public event EventHandler CanExecuteChanged = delegate { };
  
          void ICommand.Execute(object parameter)
          {
              if (_TargetExecuteMethod != null)
              {
                  _TargetExecuteMethod();
              }
          }
          #endregion
      }
  </code></pre>
  
  <p>Here is the other half of the code in the same RelayCommand class which never gets executed when debugging. I think that the below code needs to get executed because of my parameter.</p>
  
  <pre><code>public class RelayCommand&lt;T&gt; : ICommand
      {
          Action&lt;T&gt; _TargetExecuteMethod;
          Func&lt;T, bool&gt; _TargetCanExecuteMethod;
  
          public RelayCommand(Action&lt;T&gt; executeMethod, Func&lt;T,bool&gt; canExecuteMethod)
          {
              _TargetExecuteMethod = executeMethod;
              _TargetCanExecuteMethod = canExecuteMethod;
          }
  
          public void RaiseCanExecuteChanged() 
          {
               CanExecuteChanged(this, EventArgs.Empty); 
          }
          #region ICommand Members
  
          bool ICommand.CanExecute(object parameter)
          {
              if (_TargetCanExecuteMethod != null)
              {
                  T tparm = (T)parameter;
                  return _TargetCanExecuteMethod(tparm);
              }
              if (_TargetExecuteMethod != null)
              {
                  return true;
              }
              return false;
          }
  
          // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command
          // Prism commands solve this in their implementation
          public event EventHandler CanExecuteChanged = delegate { };
  
          void ICommand.Execute(object parameter)
          {
              if (_TargetExecuteMethod != null)
              {
                  _TargetExecuteMethod((T)parameter);
              }
          }
          #endregion
      }
  </code></pre>