title: WPF: Passing values to Validation Rules from bound data
body: <p>Quick question.
  I have a validator configured in WPF that checks to ensure that a value is within a specific range. This works great. See code below:</p>
  
  <pre><code>&lt;TextBox Name="valueTxt" Style="{StaticResource SquareBox}" GotKeyboardFocus="amountTxt_GotKeyboardFocus" GotMouseCapture="amountTxt_GotMouseCapture" LostFocus="boxLostFocus" Height="25" Width="50"&gt;
                              &lt;TextBox.Text&gt;
                                  &lt;Binding Path="UnitCost" NotifyOnValidationError="True"&gt;
                                      &lt;Binding.ValidationRules&gt;
                                          &lt;local:ValidDecimal MaxAmount="1000"&gt;&lt;/local:ValidDecimal&gt;
                                      &lt;/Binding.ValidationRules&gt;
                                      &lt;Binding.Converter&gt;
                                          &lt;local:CurrencyConverter addPound="False" /&gt;
                                      &lt;/Binding.Converter&gt;
                                  &lt;/Binding&gt;
                              &lt;/TextBox.Text&gt;
                          &lt;/TextBox&gt;
  </code></pre>
  
  <p>However, I would like to pass the validator another piece of data from the data that is being bound. I assumed that I could just add this to the delcaration of the validator like so:</p>
  
  <pre><code>&lt;local:ValidDecimal MaxAmount="1000" SKU="{Binding Path=tblProducts.ProductSKU}"&gt;&lt;/local:ValidDecimal&gt;
  </code></pre>
  
  <p>However, is appears that I can't access the SKU value in this way.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks,</p>
  
  <p>EDIT
  May well be worth pointing out that SKU is simply a string declared in my validator, like so:</p>
  
  <pre><code>public class ValidDecimal : ValidationRule
  {
      public int MaxAmount { get; set; }
      public string SKU { get; set; }
  
      public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
      {
          //validate the value as a decimal in to two decimal places
          string cost = (string)value;
          try
          {
  
              decimal decCost = decimal.Parse(cost);
              if (SKU != "85555")
              {
                  if (decCost &lt;= 0 || decCost &gt; MaxAmount)
                  {
                      return new ValidationResult(false, "Amount is not within valid range");
                  }
              }
              return new ValidationResult(true, null);
  
          }
          catch (Exception ex)
          {
              return new ValidationResult(false, "Not a valid decimal value");
          }
      }
  }
  </code></pre>