title: WPF validation rule preventing decimal entry in textbox?
body: <p>I have a WPF textbox defined in XAML like this:</p>
  
  <pre><code>&lt;Window.Resources&gt;        
      &lt;Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="Validation.HasError" Value="true"&gt;
                  &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  
  &lt;TextBox x:Name="upperLeftCornerLatitudeTextBox" Style="{StaticResource textBoxInError}"&gt;
      &lt;TextBox.Text&gt;
          &lt;Binding Path="UpperLeftCornerLatitude" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"&gt;
              &lt;Binding.ValidationRules&gt;
                  &lt;local:LatitudeValidationRule ValidationStep="RawProposedValue"/&gt;
              &lt;/Binding.ValidationRules&gt;
          &lt;/Binding&gt;
      &lt;/TextBox.Text&gt;
  &lt;/TextBox&gt;
  </code></pre>
  
  <p>As you can see, my textbox is bound to a decimal property on my business object called UpperLeftCornerLatitude which looks like this:</p>
  
  <pre><code>private decimal _upperLeftCornerLongitude;
  public decimal UpperLeftCornerLatitude
  {
      get { return _upperLeftCornerLongitude; }
      set
      {
          if (_upperLeftCornerLongitude == value)
          {
              return;
          }
  
          _upperLeftCornerLongitude = value;
          OnPropertyChanged(new PropertyChangedEventArgs("UpperLeftCornerLatitude"));
      }
  }
  </code></pre>
  
  <p>My user will be entering a latitude value into this textbox and in order to validate that entry, I've created a validation rule that looks like this:</p>
  
  <pre><code>public class LatitudeValidationRule : ValidationRule
  {
      public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
      {
          decimal latitude;
  
          if (decimal.TryParse(value.ToString(), out latitude))
          {
              if ((latitude &lt; -90) || (latitude &gt; 90))
              {
                  return new ValidationResult(false, "Latitude values must be between -90.0 and 90.0.");
              }
          }
          else
          {
              return new ValidationResult(false, "Latitude values must be between -90.0 and 90.0.");
          }
  
          return new ValidationResult(true, null);
      }
  }
  </code></pre>
  
  <p>My textbox initially starts off empty and I have a breakpoint set at the beginning of my validation rule.  I enter 1 in the textbox and when my debugger breaks inside of the validation rule, I can see that value = "1".  So far so good.  Now I continue running and enter a decimal point in the textbox (so we should have "1." now).  Again, the debugger breaks inside of the validation rule and, as expected, value = "1.".  If I step through the validation rule code, I see that it passes the latitude value check and returns the following:</p>
  
  <pre><code>new ValidationRule(true, null);
  </code></pre>
  
  <p>However, as soon as the validation rule returns and I step into the next line of code, I find myself on the first line of my UpperLeftCornerLatitude property setter.  Mousing over value here reveals that it's a value of "1" instead of "1." as I would expect.  So naturally when I continue running my code, I end up back in the textbox staring at a value of "1" instead of "1.".  If I remove all of the breakpoints, the effect is that I can't seem to enter a decimal point in the textbox.  Is there something obvious that I'm missing here that's causing my setter to end up with a value of "1" even though I have entered "1." in the textbox?  Thanks very much!</p>