title: WPF MVVM command canexecute enable/disable button
body: <p>I want to enable RibbonButton when textbox property text isn't null. Disable RibbonButton when textbox property text is null. I want to use CanExecute method in ICommand for it. How can I do it?</p>
  
  <p><strong>View:</strong></p>
  
  <pre><code> &lt;Custom:RibbonButton
                          LargeImageSource="..\Shared\img\save_diskete.png"
                          Label="Save"
                          Command="{Binding ButtonCommand}"&gt;
                      &lt;/Custom:RibbonButton&gt;
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>class KomentarViewModel:BaseViewModel
      {
          #region Data
          private ICommand m_ButtonCommand;
          public ICommand ButtonCommand
          {
              get
              {
                  return m_ButtonCommand;
              }
              set
              {
                  m_ButtonCommand = value;
              }
          }
          private string textKomentar;
          public string TextKomentar
          {
              get
              {
                  return this.textKomentar;
              }
              set
              {
                  // Implement with property changed handling for INotifyPropertyChanged
                  if (!string.Equals(this.textKomentar, value))
                  {
                      textKomentar = value;
                      OnPropertyChanged("TextKomentar");
                  }
              }
          }        
          private ObservableCollection&lt;Komentar&gt; allCommentsInc;
          public ObservableCollection&lt;Komentar&gt; AllCommentsInc
          {
              get
              {
                  return allCommentsInc;
              }
              set
              {
                  allCommentsInc = value;
                  OnPropertyChanged("AllCommentsInc");
              }
          }
  
          public int idIncident { get; private set; }
          public Incident incident { get; private set; }
          #endregion
  
          #region Constructor
          public KomentarViewModel(int id)
          {
              CC_RK2Entities context = new CC_RK2Entities();
              this.idIncident = id;
  
              AllCommentsInc = new ObservableCollection&lt;Komentar&gt;(context.Komentar.Where(a =&gt; a.Incident_id == idIncident));
              incident = context.Incident.Where(a =&gt; a.id == idIncident).First();
  
              //ButtonCommand = new RelayCommand(new Action&lt;object&gt;(ShowMessage));
          }
          #endregion        
  
          #region Methods
          //ukaz napsany text
          public void ShowMessage(object obj)
          {
              //MessageBox.Show(obj.ToString());
              MessageBox.Show(this.TextKomentar);
          }
  }
  </code></pre>
  
  <p><strong>RelayCommand</strong></p>
  
  <pre><code>namespace Admin.Shared.Commands
  {
      class RelayCommand : ICommand
      {
          private Action&lt;object&gt; _action;
  
          public RelayCommand(Action&lt;object&gt; action)
          {
              _action = action;
          }
  
          #region ICommand Members
  
          public bool CanExecute(object parameter)
          {
              return true;            
          }
  
          public event EventHandler CanExecuteChanged;
  
          public void Execute(object parameter)
          {
                  _action(parameter);
          }
  
          #endregion
      }
  }
  </code></pre>