title: Enable Disable Button base on validation in WPF
body: <p>I have a WPF Form, that has a text box and a button. I am validating the text box to have only characters. Validation works fine, But I need to disable the button if there are validation error and enable them if there are no validation errors. 
  Below is my code:</p>
  
  <pre><code>&lt;TextBox Name="tbProductName" Grid.Column="1" HorizontalAlignment="Left" Height="25" Margin="4,9,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="213" 
                      Text="{Binding Path = ProductCode, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,NotifyOnValidationError=True}"&gt;               
              &lt;Validation.ErrorTemplate&gt;
                  &lt;ControlTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;AdornedElementPlaceholder x:Name="textBox"/&gt;
                          &lt;TextBlock Text="{Binding [0].ErrorContent}" Foreground="Red"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Validation.ErrorTemplate&gt;
          &lt;/TextBox&gt;
          &lt;Button Name ="btnDownload" Content="Download" Grid.Column="2" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="87" Height="24" Click="btnDownload_Click"/&gt;
  
  
  public class ViewModel: System.ComponentModel.INotifyDataErrorInfo 
     {
         private readonly Dictionary&lt;string, ICollection&lt;string&gt;&gt; _validationErrors = new Dictionary&lt;string, ICollection&lt;string&gt;&gt;();
         private readonly Model _productCode = new Model();
         public string ProductCode
         {
             get { return _productCode.ProductCode; }
             set
             {
                 _productCode.ProductCode = value;
                 ValidateModelProperty(value, "ProductCode");
             }
         }
  
         protected void ValidateModelProperty(object value, string propertyName)
         {
             if (_validationErrors.ContainsKey(propertyName))
                 _validationErrors.Remove(propertyName);
  
             PropertyInfo propertyInfo = _productCode.GetType().GetProperty(propertyName);
             IList&lt;string&gt; validationErrors =
                   (from validationAttribute in propertyInfo.GetCustomAttributes(true).OfType&lt;ValidationAttribute&gt;()
                    where !validationAttribute.IsValid(value)
                    select validationAttribute.FormatErrorMessage(string.Empty))
                    .ToList();
  
             _validationErrors.Add(propertyName, validationErrors);
             RaiseErrorsChanged(propertyName);
         }
           /* Raise the ErrorsChanged for all properties explicitly */
  
         protected void ValidateModel()
         {
             _validationErrors.Clear();
             ICollection&lt;ValidationResult&gt; validationResults = new List&lt;ValidationResult&gt;();
             ValidationContext validationContext = new ValidationContext(_productCode, null, null);
             if (!Validator.TryValidateObject(_productCode, validationContext, validationResults, true))
             {
                 foreach (ValidationResult validationResult in validationResults)
                 {
                     string property = validationResult.MemberNames.ElementAt(0);
                     if (_validationErrors.ContainsKey(property))
                     {
                         _validationErrors[property].Add(validationResult.ErrorMessage);
                     }
                     else
                     {
                         _validationErrors.Add(property, new List&lt;string&gt; { validationResult.ErrorMessage });
                     }
                 }
             }
  
             /* Raise the ErrorsChanged for all properties explicitly */
             RaiseErrorsChanged("ProductCode");
         }
  
         #region INotifyDataErrorInfo members
         public event EventHandler&lt;System.ComponentModel.DataErrorsChangedEventArgs&gt; ErrorsChanged;
         private void RaiseErrorsChanged(string propertyName)
         {
             if (ErrorsChanged != null)
                 ErrorsChanged(this, new System.ComponentModel.DataErrorsChangedEventArgs(propertyName));
         }
  
         public System.Collections.IEnumerable GetErrors(string propertyName)
         {
             if (string.IsNullOrEmpty(propertyName)
                 || !_validationErrors.ContainsKey(propertyName))
                 return null;
  
             return _validationErrors[propertyName];
         }
  
         public bool HasErrors
         {
             get { return _validationErrors.Count &gt; 0; }
         }
         #endregion
  
      }  
  
  public class Model
      {
          [Required(ErrorMessage = "You must enter a product code to download.")]
          [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "The Product Code must only contain letters (a-z, A-Z).")]
          public string ProductCode { get; set; }
      }
  </code></pre>
  
  <p>How can I set the isEnabled property of the button to Validation.HasErrors?</p>