RSS

Validating Binding

23 Feb

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.

Advertisements
 
Leave a comment

Posted by on February 23, 2012 in WPF

 

Tags: ,

Leave a Reply

Please log in using one of these methods to post your comment:

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

 
%d bloggers like this: