Basic data validation in Silverlight

Source: Internet
Author: User
Tags html page integer silverlight

Silverlight 2 supports basic data validation capabilities. In Silverlight 2, when we bind data to a UI control, the validation rules that the data has are automatically bound to the UI control.

For example, a data field is set to an integer type, and an error occurs when we update the field with a non-integer data. We can use this rule to validate input data in the UI. To do this, we simply set two XAML properties and implement the desired UI behavior in the defined event.

For example, the following XAML code defines a set of controls in which the user updates the data through a textbox:

﹤StackPanel x:Name="dataForm"﹥
 ﹤TextBlock Text="FirstName" Width="125"  FontSize="12" /﹥
 ﹤TextBox x:Name="FirstNameTextBox" IsReadOnly="False"  Width="150" 
Text="{Binding FirstName, Mode=TwoWay, NotifyOnValidationError=true,  ValidatesOnExceptions=true}"
BindingValidationError="FirstNameTextBox_BindingValidationEr ror"/﹥
 ﹤/StackPanel﹥ 

When both the NotifyOnValidationError and Validatesonexceptions properties are set to True, Silverlight The Bindingvalidationerror event is triggered when an error occurs on the rule that the input data validation is bound to, so that we are notified when an error occurs in the validation.

What we're going to do next is to implement the behavior of the event trigger, which is the UI behavior we expect. Such as:

private void FirstNameTextBox_BindingValidationError(object sender,  ValidationErrorEventArgs e)
{
  if (e.Action ==  ValidationErrorEventAction.Added)
  {
    ((Control)e.OriginalSource).Background  = new SolidColorBrush(Colors.Red);
    this.Dispatcher.BeginInvoke(() =>HtmlPage.Window.Alert("The input format is invalid"));
  }
  if  (e.Action == ValidationErrorEventAction.Removed)
  {
    ((Control) e.OriginalSource).Background = new SolidColorBrush(Colors.White);
  }
} 

In the above code, when the error occurs, the background color of the control (this is the TextBox) turns red and the user is prompted with a warning window for the HTML page. When the error is corrected, the space is restored to the default white.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.