title: Toggle "CanExecute" of a button based on grid selection
body: <p>I am very to Modern UI Programming and now i got stuck in a small C# WPF Application which is basically a learning project of MVVM design pattern.</p>
  
  <p>I have a DataGrid and some Buttons to handle data operation (add, edit, delete).</p>
  
  <p>What i want to achieve: the edit button should not be enable, when no row in grid is selected. </p>
  
  <p><strong>edit button:</strong></p>
  
  <pre><code>&lt;Button Width="126" Height="22" Content="Edit" Margin="5,5,5,5" Command="{Binding KontoEdit}" /&gt;
  </code></pre>
  
  <p><strong>grid:</strong></p>
  
  <pre><code>   &lt;DataGrid ItemsSource="{Binding Konten}"  SelectedItem="{Binding SelectedKonto}"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=KtoNr}" Header="Nr" IsReadOnly="True" /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True"  /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=KtoArt}" Header="Kontoart" IsReadOnly="True"  /&gt;
              &lt;DataGridTextColumn Binding="{Binding Path=KtoKlasse}" Header="Kontenklasse" IsReadOnly="True"  /&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>view model:</strong></p>
  
  <pre><code>public class MainViewModel : INotifyPropertyChanged
  {
      KontenDB ctx = new KontenDB();
  
      public MainViewModel()
      {
          FillKonten();
  
          CanKontoEditExecute = true ;
  
          KontoEdit = new RelayCommand(o =&gt; { DoKontoEdit(SelectedKonto); }, param =&gt; CanKontoEditExecute);
      }
  
  
      #region //Commands
      public void DoKontoEdit(Konten k)
      {
          //Edit the Selected Item
      }
  
      private ICommand _kontoEdit;
      public ICommand KontoEdit
      {
          get
          {
              return _kontoEdit;
          }
          set
          {
              _kontoEdit = value;
          }
      }
  
      private bool _canKontoEditExecute;
  
      public bool CanKontoEditExecute
      {
          get
          {
              return _canKontoEditExecute;
          }
          set
          {
              _canKontoEditExecute = value;
          }
      }
  
      #endregion //Commands
      private void FillKonten()
      {
          var q = (from k in ctx.Konten
                   select k).ToList();
          Konten = new ObservableCollection&lt;Konten&gt;(q);
      }
  
  
      private ObservableCollection&lt;Konten&gt; _konten;
      public ObservableCollection&lt;Konten&gt; Konten
      {
          get
          {
              return _konten;
          }
          set
          {
              _konten = value;
              NotifyPropertyChanged();
          }
      }
  
      private Konten _selectedKonto;
      public Konten SelectedKonto
      {
          get
          {
              return _selectedKonto;
          }
          set
          {
              _selectedKonto = value;
              NotifyPropertyChanged();
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>model is generated by EF6.</p>
  
  <p><strong>edit: RelayCommand class:</strong></p>
  
  <pre><code>public class RelayCommand : ICommand
      {
          private Action&lt;object&gt; execute;
  
          private Predicate&lt;object&gt; canExecute;
  
          private event EventHandler CanExecuteChangedInternal;
  
          public RelayCommand(Action&lt;object&gt; execute)
              : this(execute, DefaultCanExecute)
          {
          }
  
          public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute)
          {
              if (execute == null)
              {
                  throw new ArgumentNullException("execute");
              }
  
              if (canExecute == null)
              {
                  throw new ArgumentNullException("canExecute");
              }
  
              this.execute = execute;
              this.canExecute = canExecute;
          }
  
          public event EventHandler CanExecuteChanged
          {
              add
              {
                  CommandManager.RequerySuggested += value;
                  CanExecuteChangedInternal += value;
              }
  
              remove
              {
                  CommandManager.RequerySuggested -= value;
                  CanExecuteChangedInternal -= value;
              }
          }
  
          public bool CanExecute(object parameter)
          {
              return canExecute != null &amp;&amp; canExecute(parameter);
          }
  
          public void Execute(object parameter)
          {
              execute(parameter);
          }
  
          public void OnCanExecuteChanged()
          {
              EventHandler handler = CanExecuteChangedInternal;
              if (handler != null)
              {
                  handler.Invoke(this, EventArgs.Empty);
              }
          }
  
          public void Destroy()
          {
              canExecute = _ =&gt; false;
              execute = _ =&gt; { return; };
          }
  
          private static bool DefaultCanExecute(object parameter)
          {
              return true;
          }
      }
  </code></pre>
  
  <p>So how can i achieve that when: no row in datagrid is selected or SelectedKonto is null the CanKontoEditExecute property changes to false?</p>
  
  <p>Thanks a lot for your help!</p>