WPF 3.5 Data Validation – Shorter Syntax

One great new feature in the .NET 3.5 Framework is, WPF now supports the IDataErrorInfo interface when data binding.  You can read an article posted by the SDK Team here Data Validation In 3.5.

I read the article and found that their example syntax looked long hand to me so I changed it to be much shorter.

SDK Example Syntax

Nested <Binding> within the <TextBox> control.

<TextBox x:Name="txtLastName">
    <Binding Path="LastName" UpdateSourceTrigger="LostFocus"
             Mode="TwoWay" ValidatesOnDataErrors="True"/>
</TextBox>

Shorter Syntax

<TextBox> control on a single line of XAML. (on two lines here to fit in lmited space)

<TextBox x:Name="txtFirstName" Text="{Binding Path=FirstName, UpdateSourceTrigger=LostFocus,
         Mode=TwoWay, ValidatesOnDataErrors=True}" />

I like this shorter syntax since it will fit in one line of XAML code and shortens the vertical length of my XAML files.  You can also add any required converters if required to this same syntax.

For those who prefer the longer syntax, go for it.  This is WPF, you can have it your way!

Just a grain of sand on the worlds beaches.

Leave a Reply

You must be logged in to post a comment.