title: WPF TextBox Binding Validation Rules not Firing on LostFocus When TextBox is Empty
body: <p>We all know how sucky WPF validation is out of the box. I am trying a very simple thing and for some reason it is always failing. I have a TextBox and my only requirement is to validate that the user inputs something in the TextBox. The TextBox is bound to a Customer object with FirstName and LastName properties. </p>
  
  <p>Here is the XAML code: </p>
  
  <pre><code>            &lt;TextBox Style="{StaticResource TextBoxStyle}" Grid.Column="1"  Grid.Row="0" Height="20" Width="100" Margin="10"&gt;
                  &lt;TextBox.Text&gt;
                      &lt;Binding Path="FirstName" &gt;
                          &lt;Binding.ValidationRules&gt;
                              &lt;ExceptionValidationRule /&gt;
                          &lt;/Binding.ValidationRules&gt;
                      &lt;/Binding&gt;
                  &lt;/TextBox.Text&gt;
  
              &lt;/TextBox&gt;
  </code></pre>
  
  <p>Here is the Customer class FirstName property: </p>
  
  <pre><code>public string FirstName
          {
              get { return _firstName;}
              set
              {
                  if(String.IsNullOrEmpty(value))
                      throw new ApplicationException("FirstName cannot be null or empty!");
                  _firstName = value; 
  
                  OnPropertyChanged("FirstName");
              }
          }
  </code></pre>
  
  <p>Even though I am throwing an exception if the FirstName (value) is null or empty it is only handled if I type something in the TextBox and then delete it and then tab off. The reason is that it is dependent on the property changed event. But even if I put that TextBox binding on Focus it does not fire the validation. </p>
  
  <p>UPDATE: </p>
  
  <p>One of the ugliest ways to handle this issue is to assign the String.Empty to the TextBoxes on the Window.Loaded event: </p>
  
  <pre><code> void AddCustomerWindow_Loaded(object sender, RoutedEventArgs e)
          {
              // get all the textboxes and set the property to empty strings! 
  
              txtFirstName.Text = String.Empty;
              txtLastName.Text = String.Empty; 
          }
  </code></pre>
  
  <p>Here is the code for binding: </p>
  
  <pre><code> public AddCustomerWindow()
          {
              InitializeComponent();
  
              this.Loaded += new RoutedEventHandler(AddCustomerWindow_Loaded);
  
              gvAddCustomer.DataContext = new Customer();  
          }
  </code></pre>