![]() ![]() Return new ValidationResult(false, $"Illegal characters or. Public override ValidationResult Validate(object value, CultureInfo cultureInfo) ![]() ![]() public class AgeRangeRule : ValidationRule The Validate method returns a ValidationResult that indicates if the value is valid based on whether an exception is caught during the parsing and whether the age value is outside of the lower and upper bounds. The Int32.Parse method is called on the value to make sure that it does not contain any invalid characters. The following example shows the implementation of AgeRangeRule, which inherits from ValidationRule and overrides the Validate method. The binding is set up to use a validation rule named AgeRangeRule so that if the user enters non-numeric characters or a value that is smaller than 21 or greater than 130, a red exclamation mark appears next to the text box and a tool tip with the error message appears when the user moves the mouse over the text box. The text content of the TextBox in the following example is bound to the Age property (of type int) of a binding source object named ods. We have successfully imlemented input validation in Silverlight 3.This example shows how to use an ErrorTemplate and a style trigger to provide visual feedback to inform the user when an invalid value is entered, based on a custom validation rule. The following figure displays when nothing is hovered. These are the error messages when you mouse over the red flag on the top right corner of the input. The following error messages will be thrown when there is an error with the user input. You will see the error messages that you have provided in the properties in a red box which can be shown when you mouse hover onto it. Press F5 to run in Debug mode or you can try with Start without Debugging (Ctrl + F5). ![]() That's it now we are ready with our demo. Now add the instance of the UserInfo class to the DataContext of the LayoutRoot Grid in.Now time to bind these properties with the Xaml controls we have.Throw new ArgumentException( "Your Age must be in the Range 18 ~ 40") Add properties and required logic to validate the user input.This.Propert圜hanged( this, new Propert圜hangedEventArgs(propertyName)) Private void RaisePropert圜hanged( string propertyName) Add a method that can notify when there is a property change.We will implement INotifyPropert圜hanged interface to view the notifications.Now we will add a class to the Silverlight Project and Name is UserInfo.cs.Now design part is done open the solution in Visual Studio Again. I have 2 Password Boxes for Password and confirm Password. The MainPage.xaml will look like as follows:Īs you see from the above figure, I have 3 text boxes for User Name, Email ID, and Age. To make the application look good I am going to design it in Blend 3, don't worry this will be a simple design. In this article you will se how we can validate the user input.įire up Visual Studio 2008 and create a Silverlight Application. So the basic question that comes to our mind is that can we achieve that in Silverlight 3. Data Validation, in an ASP.NET site you can easily implement the Data Validation using the Validation options like Required Field Validator, Range Validator so and so forth. ![]()
0 Comments
Leave a Reply. |