views:

231

answers:

2

I am new to windows phone 7 development platform.I am trying to do validation for textbox input. On debugging ,in case of invalid input , I get the error "exception was unhandled".How to correct this?This code works fine for silverlight application.

TextBox Text="{Binding Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />

private string _name;  
public String Name  
        {  
            get   {   return _name; }  
            set {

                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("invalid name");
                }
                _name = value;
                OnPropertyChanged("Name");
            }
        }
A: 

Have you tried to catch the invalid name Exception when trying to set an invalid value ?

thelost
Putting try-catch block inside property setter works ,but i have to use a message box to display the error message. what is the way to output the exception in silverlight type style where error message is displayed beside text box
rk1
A: 

I have had the same issue and found the following answer:

Make sure to not call the viewmodel's setter programmatically because in this case you have to take care of the exception, too. If you let the data binding try to update the underlying viewmodel it also handles the exception for you.

embee