1

Within an ItemsControl which is bound to a data source, I am trying to validate a TextBox using a custom validation rule, which accepts a parameter, following is the code for the vaildation rule.

public class RatioValidation : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        if (!String.IsNullOrEmpty(Maturity.MaturityValue) && Helper.IsDateInCorrectFormat(Maturity.MaturityValue))
        {
            String strVal = Convert.ToString(value);
            Double ratio = 0;
            Boolean isValid = Double.TryParse(strVal, out ratio);
            if (isValid)
            {
                if (ratio <= 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));

}

And then use it like following,

<TextBox Grid.Row="1" Grid.Column="2"  Name="txtRatio" BorderThickness="0" Width="148" Tag="{Binding StrMaturity, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.Text>
        <Binding Path="Ratio" Mode="TwoWay"  UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <leg:RatioValidation ValidatesOnTargetUpdated="True">
                    <leg:RatioValidation.Maturity>
                         <leg:MaturityDependencyForValidation MaturityValue="{Binding txtMaturity}"/>
                    </leg:RatioValidation.Maturity>
                </leg:RatioValidation>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

I get an exception in the output window:

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')

And within the RatioValidation rule, the value of Maturity never gets bound from the data binding. I need this value for data binding. Please help.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Vinay Dwivedi
  • 767
  • 2
  • 11
  • 23

1 Answers1

1

There is no DataContext as the surrounding objects, the binding at the very least, are not DependencyObjects, you are somewhat limited in your choices, it should be similar to what happens when binding in an array, also see this answer.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • What's the work around? I want to validated individual (UserControl) items in the ItemsControl which is bound to a collection. The usercontrol generates one row for each item in the collection. Its something like a datagrid scenario where we can specify the rowvalidation property and validate individual row items. ANy suggestions please? – Vinay Dwivedi Feb 07 '12 at 14:08
  • @VinayDwivedi: How about looking at the linked question? – H.B. Feb 07 '12 at 14:08
  • Thanks that solved my problem... Looked at http://stackoverflow.com/questions/8886309/templatebinding-not-working-on-validationrules-dependency-property... And also it looks like your article... but providing the link would have helped... thanks so much anyways. – Vinay Dwivedi Feb 10 '12 at 03:29
  • @VinayDwivedi: Both answers on both questions i linked to use the same techniques and one of the links has been there from the very start, you just didn't care to follow it... – H.B. Feb 10 '12 at 07:25