title: How can I invoke a command on the ViewModel on SelectionChanged of a ListView?
body: <p>In a MVVM/WPF environment, I want to invoke a command (<code>ComputeCommand</code>) on the ViewModel when the <code>SelectionChanged</code> event of a ListView is raised. How can this be done, either in XAML or in C#?</p>
  
  <p>Here is my command class. I have tried <code>MainViewModel.Instance.MyCommand.Execute();</code> in the codebehind, but it's doesn't accept that.</p>
  
  <pre><code>public class ComputeCommand : ICommand
  {
      public ComputeCommand(Action updateReport)
      {
          _executeMethod = updateReport;
      }
  
      Action _executeMethod;
  
      public bool CanExecute(object parameter)
      {
          return true;
      }
  
      public event EventHandler CanExecuteChanged;
  
      public void Execute(object parameter)
      {
          _executeMethod.Invoke();
      }
  }       
  </code></pre>