Enable / Disable Binding Validation

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 .

Advertisements

3 comments

  1. Thanks for your post. Your code help me.

    Can you post code relative to:

    “I created some extention methods in order to quickly and transparently enable or disable validation rules at will.”

    I seach how use the IsActive property.

  2. First of all, Enabling/Disabling is supported only for Rules that are subclassed from BaseValidationRule

    So for every rule in a binding you must first check it that rule is of type BaseValidationRule

    The path to follow is BindingExpresion->ParentBinding->ValidationRules

    Sorry can’t post entire class code due to blog limitations

  3. Excellent. I was struggling with this for some time. Here is the code. it might be helpful for others.

    Binding binding = BindingOperations.GetBinding(myCombo, ComboBox.SelectedValueProperty);

    if (binding.ValidationRules.Count > 0)
    {
    if(binding.ValidationRules[0] is BaseValidationRule)
    {
    (binding.ValidationRules[0] as BaseValidationRule).IsActive = false;
    }
    }

    You can add this bit in your code behind, on load of your control.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s