title: WPF MVVM RelayCommand Action, canExecute, parameter
body: <p>I´m using MVVM, I have defined a Command in a button. I want to use in this Command a parameter, execute an action and proving if Canexecute.</p>
  
  <p>I have this RelayCommand</p>
  
  <pre><code>class RelayCommand&lt;T&gt; : ICommand
      {
          private readonly Action&lt;T&gt; _execute;
          private readonly Func&lt;T, bool&gt; _canExecute;
  
          /// &lt;summary&gt;
          /// Initializes a new instance of the RelayCommand class that 
          /// can always execute.
          /// &lt;/summary&gt;
          /// &lt;param name="execute"&gt;The execution logic.&lt;/param&gt;
          /// &lt;exception cref="ArgumentNullException"&gt;If the execute argument is null.&lt;/exception&gt;
          public RelayCommand(Action&lt;T&gt; execute)
              : this(execute, null)
          {
          }
  
          /// &lt;summary&gt;
          /// Initializes a new instance of the RelayCommand class.
          /// &lt;/summary&gt;
          /// &lt;param name="execute"&gt;The execution logic.&lt;/param&gt;
          /// &lt;param name="canExecute"&gt;The execution status logic.&lt;/param&gt;
          /// &lt;exception cref="ArgumentNullException"&gt;If the execute argument is null.&lt;/exception&gt;
          public RelayCommand(Action&lt;T&gt; execute, Func&lt;T, bool&gt; canExecute)
          {
              if (execute == null)            
                  throw new ArgumentNullException("execute");
  
              _execute = execute;
  
              if (canExecute != null)            
                  _canExecute = canExecute;            
          }
  
          #region ICommand Members
  
          public bool CanExecute(object parameter)
          {
              if (_canExecute == null)            
                  return true;
  
              if (parameter == null &amp;&amp; typeof(T).IsValueType)
                  return _canExecute(default(T));
  
              return _canExecute((T)parameter);            
          }
  
          public event EventHandler CanExecuteChanged
          {
              add { CommandManager.RequerySuggested += value; }
              remove { CommandManager.RequerySuggested -= value; }
          }
  
          public void Execute(object parameter)
          {
              _execute((T)parameter);
          }
  
          #endregion
      }
  </code></pre>
  
  <p>With this button</p>
  
  <pre><code>&lt;Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
          Style="{StaticResource BotonSelect}" Width="200"
          Command="{Binding ModificarLicenciaCommand}"                          &gt;
          &lt;Label Content="Modificar Licencia" /&gt;
  &lt;/Button&gt;
  </code></pre>
  
  <p>And in the View Model.</p>
  
  <pre><code>ModificarLicenciaCommand = new RelayCommand(ModificarLicencia, CanModificarLicencia);
  
  private bool CanModificarLicencia()
  {
     // Comprobar puedo modificar                             
     return true;
  }
  
  private void ModificarLicencia()
  {
      // Modificar licencia
  }
  </code></pre>
  
  <p>This is Ok, but I want to pass a parameter and using something like this:</p>
  
  <p><strong>CommandParameter="{Binding ElementName=DataGridLicencias}"</strong></p>
  
  <pre><code>&lt;Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
      Style="{StaticResource BotonSelect}" Width="200"
      Command="{Binding ModificarLicenciaCommand}"
      CommandParameter="{Binding ., ElementName=DataGridLicencias}" &gt;
      &lt;Label Content="Modificar Licencia" /&gt;
  &lt;/Button&gt;
  </code></pre>
  
  <p>and in viewModel: </p>
  
  <p>RelayCommand&lt; <strong>SfDataGrid</strong> >   </p>
  
  <pre><code>ModificarLicenciaCommand = new RelayCommand&lt;SfDataGrid&gt;(ModificarLicencia, CanModificarLicencia);
  
  private void ModificarLicencia(SfDataGrid dataGrid)
  {
      // Modificar licencia
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>With this, I have an error in ModificarLicenciaCommand = new RelayCommand(ModificarLicencia, CanModificarLicencia)</p>
  
  <p>In CanModificarLicentia ==> Error Argument2: cannot convert from 'method group' to 'Func'</p>
  
  <p>Any help?</p>