RSS

Tag Archives: Data Binding

Validating Binding

Writing XAML Code is very cool, but if you have a control in which you have to write several times

<TextBox Text="{Binding Path=... ValidatesOnDataErrors = true, 
                ValidatesOnException = true, 
                UpdateSourceTrigger = PropertyChanged}" />

might be very stressul and unclear in case of searching errors. Therefore I have created following class, called ValidatingBinding, that inherits from Binding and sets at creation time the properties ValidatesOnDataError and ValidatesOnExceptions to true

public class ValidatingBinding : Binding
{
    public ValidatingBinding()
    {
        ValidatesOnDataErrors = true;
        ValidatesOnExceptions = true;
        //and if needed (be careful, might be a performance issue)
        //UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    }
}

Doing that you will be able to write following in XAML and omit to set the properties ValidatesOnDataError and ValidatesOnExceptions to true.

<TextBox Text="{namespace:ValidatingBinding Path=... }"/>

As you can see, this is much shorther than the previous example. Reviewing such XAML Code is more clear if you do not rewrite such duplicate property setting.

 
Leave a comment

Posted by on February 23, 2012 in WPF

 

Tags: ,