title: Custom Validation Rule WPF and Items control
body: <p>Within an <code>ItemsControl</code> which is bound to a data source, I am trying to validate a <code>TextBox</code> using a custom validation rule, which accepts a parameter, following is the code for the vaildation rule.</p>
  
  <pre><code>public class RatioValidation : ValidationRule
  {
      public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
      {
          if (!String.IsNullOrEmpty(Maturity.MaturityValue) &amp;&amp; Helper.IsDateInCorrectFormat(Maturity.MaturityValue))
          {
              String strVal = Convert.ToString(value);
              Double ratio = 0;
              Boolean isValid = Double.TryParse(strVal, out ratio);
              if (isValid)
              {
                  if (ratio &lt;= 0)
                      return new ValidationResult(false, "Please enter a valid ratio!");
                  else return new ValidationResult(true, null);
              }
              return new ValidationResult(false, "Please enter a valid ratio!");
          }
          else return new ValidationResult(true, null);           
      }
  
      private MaturityDependencyForValidation _maturity;
      public MaturityDependencyForValidation Maturity
      {
          get { return _maturity; }
          set { _maturity = value; }
      }
  }
  
  public class MaturityDependencyForValidation : DependencyObject
  {
      public String MaturityValue
      {
          get { return (String)GetValue(MaturityValueProperty); }
          set { SetValue(MaturityValueProperty, value); }
      }
  
      public static readonly DependencyProperty MaturityValueProperty =
          DependencyProperty.Register("MaturityValue", typeof(String), typeof(MaturityDependencyForValidation), new UIPropertyMetadata(null));
  
  }
  </code></pre>
  
  <p>And then use it like following,</p>
  
  <pre><code>&lt;TextBox Grid.Row="1" Grid.Column="2"  Name="txtRatio" BorderThickness="0" Width="148" Tag="{Binding StrMaturity, UpdateSourceTrigger=PropertyChanged}"&gt;
      &lt;TextBox.Text&gt;
          &lt;Binding Path="Ratio" Mode="TwoWay"  UpdateSourceTrigger="PropertyChanged"&gt;
              &lt;Binding.ValidationRules&gt;
                  &lt;leg:RatioValidation ValidatesOnTargetUpdated="True"&gt;
                      &lt;leg:RatioValidation.Maturity&gt;
                           &lt;leg:MaturityDependencyForValidation MaturityValue="{Binding txtMaturity}"/&gt;
                      &lt;/leg:RatioValidation.Maturity&gt;
                  &lt;/leg:RatioValidation&gt;
              &lt;/Binding.ValidationRules&gt;
          &lt;/Binding&gt;
      &lt;/TextBox.Text&gt;
  &lt;/TextBox&gt;
  </code></pre>
  
  <p>I get an exception in the output window:</p>
  
  <blockquote>
    <p>Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=txtMaturity; DataItem=null; target element is 'MaturityDependencyForValidation' (HashCode=63478077); target property is 'Maturity' (type 'String')</p>
  </blockquote>
  
  <p>And within the <code>RatioValidation</code> rule, the value of <code>Maturity</code> never gets bound from the data binding. I need this value for data binding. Please help.</p>