Vinay Dwivedi
Vinay Dwivedi

Reputation: 767

Custom Validation Rule WPF and Items control

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.

Upvotes: 1

Views: 2606

Answers (1)

brunnerh
brunnerh

Reputation: 185445

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.

Upvotes: 1

Related Questions