Validation in Silverlight and WPF using ValidatesOnExceptions
Posted on | July 1, 2010 | 2 Comments
With the application as it is the user is able to enter non-numeric values and add them together.
Clicking the calculate button to add two non-numeric values together will cause the application to do nothing and the user will have no idea why.
Try it for yourself here.
It would be common sense to validate what the user has entered and let them know if and what the problem is.
What I’ve seen most beginners do for validation in Silverlight and WPF
One of easiest way (in terms of the amount of code you have to write) to do validation in Silverlight and WPF is to use exceptions.
To do this we need to make changes in the ViewModel and the Calculator XAML file.
In the ViewModel an exception needs to be thrown when the input value cannot be converted to a number.
public string FirstValue
{
get { return _firstValue; }
set
{
_firstValue = value;
try
{
int.Parse(_firstValue);
}
catch (Exception)
{
throw;
}
OnPropertyChanged("FirstValue");
}
}
In the XAML file we set ValidatesOnExceptions equal to true for the text box binding.
<TextBox Grid.Column="0" Text="{Binding FirstValue, Mode=TwoWay, ValidatesOnExceptions=True}" Height="25" TextAlignment="Right"/>
The screenshot below shows what happens if the user does not enter a number into the text box.

As you can see error message isn’t very user friendly.
To resolve this issue we could set the message of the exception thrown when parsing a number but we would be repeating/duplicating code when validating the SecondValue.
public string FirstValue
{
get { return _firstValue; }
set
{
_firstValue = value;
try
{
int.Parse(_firstValue);
}
catch (Exception)
{
throw new Exception("That's not a number");
}
OnPropertyChanged("FirstValue");
}
}
public string SecondValue
{
get { return _secondValue; }
set
{
_secondValue = value;
try
{
int.Parse(_firstValue);
}
catch (Exception)
{
throw new Exception("That's not a number");
}
OnPropertyChanged("SecondValue");
}
}
Silverlight validations the Don’t Repeat Yourself Way
A better way would to do this is by creating a reusable method for validating the users input.
void ValidateNumber(string value)
{
try
{
int.Parse(value);
}
catch (Exception)
{
throw new Exception("That's not a number");
}
}
which could be used like this
public string FirstValue
{
get { return _firstValue; }
set
{
_firstValue = value;
ValidateNumber(value);
OnPropertyChanged("FirstValue");
}
}
public string SecondValue
{
get { return _secondValue; }
set
{
_secondValue = value;
ValidateNumber(value);
OnPropertyChanged("SecondValue");
}
}
Now when a user enters an invalid number the they would see an error that looks like this

So what have we achieved?
With a few lines of code we are able to validate the users input and let them know what the problem is if it’s not valid.
If you have arrived here from a search engine, this post is part of series about refactoring Silverlight applications.
So if you’re thinking, this is all very well but the user is still able to click the calculate button even if they have entered invalid values to add together, see how this can prevented in the next part of the series.
Comments
2 Responses to “Validation in Silverlight and WPF using ValidatesOnExceptions”
Leave a Reply
July 30th, 2010 @ 5:26 pm
I’ve had some great results using the DataForm control combined with DataAnnotations. Much better than coding all of the validation by hand.
August 2nd, 2010 @ 9:35 pm
Brian thanks for the comment and I agree data annotations can save you the effort of writing validation code yourself.
I’m planing to do a post about using data annotations in the next couple of weeks.