title: Validating several linked databound TextBox values in WPF
body: <p>I am trying to find an elegant way to validate two related TextBox values in WPF.</p>
  
  <p>The Text property of each text box is bound to a public decimal property on my class which implements INotifyPropertyChanged for TwoWay binding.</p>
  
  <p>I want to validate the two values such that: BiggerValue >= SmallerValue >= 0</p>
  
  <p>I have succeeded in getting each value to validate independently against these conditions using IDataErrorInfo and a string indexer.</p>
  
  <p>My problem is as follows: the user intends to reduce both values and starts with BiggerValue, so that now it is less than SmallerValue. The validation on the BiggerValue TextBox fails (although the value is stored). The user then moves on to the SmallerValue and sets it less than the new BiggerValue. Now both values are valid again, but how can I get the BiggerValue text box to automatically reflect that its (unchanged) value is now valid?</p>
  
  <p>Should I be looking at an event handler like LostFocus() on the text boxes, or adding something like this to the property setters to force a refresh?</p>
  
  <pre><code>biggerValueTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
  smallerValueTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
  </code></pre>
  
  <p>My full code is below. Somehow it all feels rather clunky and overcomplicated for this simple problem. As a WPF newbie (this is day 2), any comments about my approach, however radical, would be gratefully received.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;TextBox x:Name="biggerValueTextBox"
           Text="{Binding Path=BiggerValue,
                          Mode=TwoWay,
                          ValidatesOnDataErrors=True,
                          ValidatesOnExceptions=True}" /&gt;
  &lt;TextBox x:Name="smallerValueTextBox"
           Text="{Binding Path=SmallerValue,
                          Mode=TwoWay,
                          ValidatesOnDataErrors=True,
                          ValidatesOnExceptions=True}" /&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>public partial class MyClass : UserControl,
      INotifyPropertyChanged, IDataErrorInfo
  {
      // properties
      private decimal biggerValue = 100;
      public decimal BiggerValue
      {
          get
          {
              return biggerValue;
          }
          set
          {
              biggerValue = value;
              OnPropertyChanged("BiggerValue");
          }
      }
      private decimal smallerValue = 80;
      public decimal SmallerValue
      {
          get
          {
              return smallerValue;
          }
          set
          {
              smallerValue = value;
              OnPropertyChanged("SmallerValue");
          }
      }
  
      // error handling
      public string this[string propertyName]
      {
          get
          {
              if (propertyName == "BiggerValue")
              {
                  if (BiggerValue &lt; SmallerValue)
                      return "BiggerValue is less than SmallerValue.";
                  if (BiggerValue &lt; 0)
                      return "BiggerValue is less than zero.";
              }
              if (propertyName == "SmallerValue")
              {
                  if (BiggerValue &lt; SmallerValue)
                      return "BiggerValue is less than SmallerValue.";
                  if (SmallerValue &lt; 0)
                      return "SmallerValue is less than zero.";
              }
              return null;
          }
      }
      // WPF doesn't use this property.
      public string Error { get { return null; } }
  
      // event handler for data binding
      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
  }
  </code></pre>