Wednesday, July 22, 2009

New features in Silverlight 3: Binding & Validation

Silverlight 3 has enhanced support for binding and data entry validation. I’ll describe it.

Binding

In Silverlight 3 you can use the ElementName property or the RelativeSource property to specify the binding source.

The ElementName property is useful when you are binding to other elements in your application.

The RelativeSource property is useful when the binding is specified in a ControlTemplate or a Style. RelativeSource markup extension has two modes:  Self or TemplatedParent. The Self mode is useful for cases where the same element should be used as the source object and target object for a binding, but different properties are the source and the target. The TemplatedParent mode is used for a data validation scenario where a template can establish the UI for error handling that occurs at run time.

In Silverlight 3 you can disable automatic source updates and update the source at times of your choosing. To do it set the UpdateSourceTrigger property to Explicit.

Validation

Silverlight 3 supports basic data validation in TwoWay bindings for target-to-source updates. Silverlight provides visual feedback for validation errors when you set the ValidatesOnExceptions property to true on the binding object. In order to receive notification that a validation error has occurred, you must also set the NotifyOnValidationError property to true on the binding object.

When you use data classes in your application, you can apply attributes to the class or members that specify validation rules, specify how the data is displayed, and set relationships between entity classes. The System.ComponentModel.DataAnnotations namespace contains the classes that are used as data attributes.

The validation and display attributes are automatically applied when used with the DataGrid control.

The ValidationSummary control displays a consolidated list of validation errors for a given container.

For more information about binding and validation look here:

I have tested all features described in the post. There are three samples in the zip file.

validation1-sh

validation2-sh 

No comments: