Today a came across a situation where I need to stop a binding from validating through the validation rules.
There is no easy way to do this. One should think that you can change the hole binding etc etc.
After doing a little snipping while in debug, I saw that the BindingExpression return from BindingExpression GetBindingExpression(DependencyPropertydp) holds in the ParentBinding.ValidationRules the validation rules declared in xaml or not.
So I decided to create a BaseValidationRule from which all validation rules will subclass.
public abstract classBaseValidationRule:ValidationRule
{
protected BaseValidationRule()
{
IsActive = true;
}
public bool IsActive { get; set; }
protected abstract ValidationResult DoValidate(objectvalue,
System.Globalization.CultureInfocultureInfo);
public overrideValidationResult Validate(objectvalue,
System.Globalization.CultureInfo cultureInfo)
{
if(!IsActive)
{
returnValidationResult.ValidResult;
}
return DoValidate(value, cultureInfo);
}
}
As you can see if the rule is not active then it will simply return a ValidationResult.ValidResult so everything will be as though as you didn’t have validation rules in xaml.
I created some extention methods in order to quickly and transparently enable or disable validation rules at will.
Note that in order for the binding to be reevaluated based on the new status of your validation rules, you need to call UpdateSource on you BindingExpression .