title: Enable button based on TextBox value (WPF)
body: <p>This is MVVM application. There is a window and related view model class. </p>
  
  <p>There is <code>TextBox</code>, <code>Button</code> and <code>ListBox</code> on form. Button is bound to <code>DelegateCommand</code> that has <code>CanExecute</code> function. Idea is that user enters some data in text box, presses button and data is appended to list box.</p>
  
  <p>I would like to enable command (and button) when user enters correct data in <code>TextBox</code>. Things work like this now:  </p>
  
  <ul>
  <li><code>CanExecute()</code> method contains code that checks if data in property bound to text box is correct.</li>
  <li>Text box is bound to property in view model </li>
  <li><code>UpdateSourceTrigger</code> is set to <code>PropertyChanged</code> and property in view model is updated after each key user presses.  </li>
  </ul>
  
  <p>Problem is that <code>CanExecute()</code> does not fire when user enters data in text box. It doesn't fire even when text box lose focus.</p>
  
  <p>How could I make this work?</p>
  
  <p><strong>Edit:</strong><br>
  Re Yanko's comment:<br>
  Delegate command is implemented in MVVM toolkit template and when you create new MVVM project, there is Delegate command in solution. As much as I saw in Prism videos this should be the same class (or at least very similar).</p>
  
  <p>Here is XAML snippet:</p>
  
  <pre><code>    ...
      &lt;UserControl.Resources&gt;
        &lt;views:CommandReference x:Key="AddObjectCommandReference" 
                                Command="{Binding AddObjectCommand}" /&gt;
     &lt;/UserControl.Resources&gt;
  
     ...
     &lt;TextBox Text="{Binding ObjectName, UpdateSourceTrigger=PropertyChanged}"&gt; &lt;/TextBox&gt;
     &lt;Button Command="{StaticResource AddObjectCommandReference}"&gt;Add&lt;/Button&gt;
     ...
  </code></pre>
  
  <p>View model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>   // Property bound to textbox
     public string ObjectName
      {
          get { return objectName; }
          set { 
              objectName = value;
              OnPropertyChanged("ObjectName");
          }
      }
  
  
      // Command bound to button
      public ICommand AddObjectCommand
      { 
          get 
          {
              if (addObjectCommand == null)
              {
                  addObjectCommand = new DelegateCommand(AddObject, CanAddObject);
              }
              return addObjectCommand;
          } 
      }
  
      private void AddObject()
      {
          if (ObjectName == null || ObjectName.Length == 0)
              return;
          objectNames.AddSourceFile(ObjectName);
          OnPropertyChanged("ObjectNames"); // refresh listbox
      }
  
      private bool CanAddObject()
      {
          return ObjectName != null &amp;&amp; ObjectName.Length &gt; 0;
      }
  </code></pre>
  
  <p>As I wrote in the first part of question, following things work:</p>
  
  <ul>
  <li>property setter for <code>ObjectName</code> is triggered on every keypress in textbox</li>
  <li>if I put <code>return true;</code> in <code>CanAddObject()</code>, command is active (button to) </li>
  </ul>
  
  <p>It looks to me that binding is correct.</p>
  
  <p>Thing that I don't know is how to make <code>CanExecute()</code> fire in setter of <code>ObjectName</code> property from above code.</p>
  
  <p>Re Ben's and Abe's answers:  </p>
  
  <ul>
  <li><p><code>CanExecuteChanged()</code> is event handler and compiler complains: </p>
  
  <blockquote>
    <p>The event
    'System.Windows.Input.ICommand.CanExecuteChanged'
    can only appear on the left hand side
    of += or -=    </p>
  </blockquote></li>
  <li><p>there are only two more members of <code>ICommand</code>: <code>Execute()</code> and <code>CanExecute()</code></p></li>
  </ul>
  
  <p>Do you have some example that shows how can I make command call <code>CanExecute()</code>.</p>
  
  <p>I found command manager helper class in <code>DelegateCommand.cs</code> and I'll look into it, maybe there is some mechanism that could help.</p>
  
  <p>Anyway, idea that in order to activate command based on user input, one needs to "nudge" command object in property setter code looks clumsy. It will introduce dependencies and one of big points of MVVM is reducing them.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>I tried to activate <code>CanExecute</code> by calling <code>addObjectCommand.RaiseCanExecuteChanged()</code> to <code>ObjectName</code> property setter from above code. This does not help either. <code>CanExecute()</code> is fired few times when form is initialized, but after that it never gets executed again. This is the code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>   // Property bound to textbox
     public string ObjectName
      {
          get { return objectName; }
          set { 
              objectName = value;
              addObjectCommand.RaiseCanExecuteChanged();              
              OnPropertyChanged("ObjectName");
          }
      }
  </code></pre>
  
  <p><strong>Edit 3: Solution</strong> </p>
  
  <p>As <a href="https://stackoverflow.com/users/63941/yanko-yankov">Yanko Yankov</a> and <a href="https://stackoverflow.com/users/48692/jerkimball">JerKimball</a> wrote, problem is static resource. When I changed button binding like Yanko suggested:</p>
  
  <pre><code>&lt;Button Command="{Binding AddObjectCommand}"&gt;Add&lt;/Button&gt;
  </code></pre>
  
  <p>things started to work immediately. I don't even need <code>RaiseCanExecuteChanged()</code>. Now <code>CanExecute</code> fires automatically.</p>
  
  <p><em>Why did I use static resource in first place?</em><br>
  Original code was from <a href="http://wpf.codeplex.com/wikipage?title=WPF%20Model-View-ViewModel%20Toolkit&amp;referringTitle=Home" rel="nofollow noreferrer">WPF MVVM toolkit</a> manual. Example in that manual defines commands as static resource and then binds it to menu item. Difference is that instead of string property in my example, MVVM manual works with <code>ObservableCollection</code>.     </p>
  
  <p><strong>Edit 4: Final explanation</strong></p>
  
  <p>I finally got it. All I needed to do was to read comment in <code>CommandReference</code> class. It says:</p>
  
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// This class facilitates associating a key binding in XAML markup to a command
  /// defined in a View Model by exposing a Command dependency property.
  /// The class derives from Freezable to work around a limitation in WPF when 
  /// databinding from XAML.
  /// &lt;/summary&gt;
  </code></pre>
  
  <p>So, <code>CommandReference</code> is used for <code>KeyBinding</code>, it is not for binding in visual elements. In above code, command references defined in resources would work for KeyBinding, which I don't have on this user control.<br>
  Of course, sample code that came with WPF MVVM toolkit were correct, but I misread it and used <code>CommandReference</code> in visual elements binding.<br>
  This WPF MVVM really is tricky sometimes.</p>