Silverlight utility tip series: 57. Binding in Silverlight (2)-data verification

Source: Internet
Author: User

This article describes how to verify Binding data in Silverlight.

Notifyonvalidationerror: Whether to trigger the bindingvalidationerror event when an exception or error message occurs.

Validatesonexceptions: Whether to display the exception information as an error message.

Validatesondataerrors: Use the idataerrorinfo interface to Display Error information.

Bindingvalidationerror: This is a routing event. When the source object a bound to data has an error, an exception is thrown to receive and trigger this event, when the source object A does not have the bindingvalidationerror event, it receives and triggers the bindingvalidationerror event of its parent object.

First, we write a user class and throw the following exception when performing a simple verification error in the attribute:Code:

     Public   Class User
{
Private String M_username;

Public String Username
{
Get { Return M_username ;}
Set
{
If (Value. Length < 3 )
{
Throw New Exception ( " The user name must be less than 3 characters long " );
}
M_username = value;
}
}

Private String M_userpwd;

Public String Userpwd
{
Get { Return M_userpwd ;}
Set
{
If (Value. Length < 6 )
{
Throw New Exception ( " Password Length cannot be less than 6 " );
}
M_userpwd = value;
}
}
}

 

Then let's take a look at the XAML code to demonstrate a Data Binding interface during logon:

<Grid X: Name ="  Layoutroot  " Background = "  White  " >
<Canvas name = " Canvasuser " Bindingvalidationerror = " Canvasuser_bindingvalidationerror "
Loaded = " Canvasuser_loaded " >
<SDK: Label Height = " 28 " Name = " Lbpwd " Width = " 55 " Canvas. Left = " 16 " Canvas. Top = " 51 "
Content = " Password: " />
<SDK: Label canvas. Left = " 16 " Canvas. Top = " 15 " Height = " 28 " Name = " Lbusername "
Width = " 55 " Content = " User name: " />
<Textbox X: Name = " Tbusername " Width = " 88 " Margin =" 10 "
TEXT = " {Binding Path = username, mode = twoway,
Notifyonvalidationerror = true, validatesonexceptions = true} "
Canvas. Left = " 91 " Canvas. Top = " 5 " />
<Textbox X: Name = " Tbuserpwd " Width = " 88 " Margin = " 10 "
TEXT = " {Binding Path = userpwd, mode = twoway,
Notifyonvalidationerror = false, validatesonexceptions = true} "
Canvas. Left = " 91 " Canvas. Top = " 45 " />
<Button Height = " 30 " Width = " 70 " Content = " Submit "
Canvas. Left = " 54 " Canvas. Top = " 93 " />
</Canvas>
</GRID>

Finally, bind the user class to the original interface at the front end, and change the textbox border to red when describing the bindingvalidationerror event.

     Public   Partial   Class Mainpage: usercontrol
{
Public Mainpage ()
{
Initializecomponent ();
}

Private Void Canvasuser_bindingvalidationerror ( Object Sender, validationerroreventargs E)
{

Textbox TB = E. originalsource As Textbox;
If (E. Action = validationerroreventaction. added)
{
TB. borderbrush = New Solidcolorbrush (colors. Red );
}
Else If (E. Action = validationerroreventaction. removed)
{
TB. borderbrush = New Solidcolorbrush (colors. White );
}
}

Private Void Canvasuser_loaded ( Object Sender, routedeventargs E)
{
This . Canvasuser. datacontext = New User ();
}
}

If the actual result is true, an error is automatically prompted when you enter a non-specific metric value. If you need the source code, click slbinding2.rar to download it.

 

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.