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.