How to check input when clicking a button in silverlight?

Im using DataAnnotation to test input controls. But ValidatesOnExceptions only works when the user types something in the text box and presses Tab. (Mostly at the Lostfocus event).

but if the user never types anything into the text box and clicks the submit button. This does not work. As an ASP.NET Page.IsValid property - is there any property or method in Silverlight that I can use that will check all the controls in the user interface?

+4
source share
2 answers

By accepting the help at the URL provided by Terence, I have prepared the solution for you below. This can be used to ensure that all properties are set before calling the service.

public class PersonViewModel : EntityBase { private readonly RelayCommand saveCommand; public PersonViewModel(IServiceAgent serviceAgent) { saveCommand = new RelayCommand(Save) { IsEnabled = true }; } public RelayCommand SaveCommand // Binded with SaveButton { get { return saveCommand; } } public String Name // Binded with NameTextBox { get { return name; } set { name = value; PropertyChangedHandler("Name"); ValidateName("Name", value); } } public Int32 Age // Binded with AgeTextBox { get { return age; } set { age = value; PropertyChangedHandler("Age"); ValidateAge("Age", value); } } private void ValidateName(string propertyName, String value) { ClearErrorFromProperty(propertyName); if (/*SOME CONDITION*/) AddErrorForProperty(propertyName, "/*NAME ERROR MESSAGE*/"); } private void ValidateAge(string propertyName, Int32 value) { ClearErrorFromProperty(propertyName); if (/*SOME CONDITION*/) AddErrorForProperty(propertyName, "/*AGE ERROR MESSAGE*/"); } public void Save() { ValidateName("Name", name); ValidateAge("Age", age); if (!HasErrors) { //SAVE CALL TO SERVICE } } } 
+1
source

I don’t think there is a way to check ALL the user controls that are visible on the page. But I would recommend you take a look at INotifyDataErrorInfo. This, in my opinion, is the best way to verify data in silverlight. With the INotifyDataErrorInfo approach, you do not need to make changes to the view (e.g. ValidatesOnException, ...), and you can easily check it using WebService (this is not possible with data annotations).

Have a look here: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-binding-with-inotifydataerrorinfo.aspx

Hope this helps you.

0
source

All Articles