The Text property of the following TextBox is data-bound to a source property Age3 that is of type int. The ExceptionValidationRule checks for exceptions that are thrown during the update of the source property (such as when the user enters a value that cannot be converted to an integer).
<TextBox Name="textBox3" Width="50" FontSize="15"
Grid.Row="4" Grid.Column="1" Margin="2"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="Age3" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
You can provide custom logic to handle those exceptions. The following example shows how to use the UpdateSourceExceptionFilter property to set an UpdateSourceExceptionFilterCallback.
BindingExpression myBindingExpression = textBox3.GetBindingExpression(TextBox.TextProperty);
Binding myBinding = myBindingExpression.ParentBinding;
myBinding.UpdateSourceExceptionFilter = new UpdateSourceExceptionFilterCallback(ReturnExceptionHandler);
myBindingExpression.UpdateSource();
The following is an example implementation of an UpdateSourceExceptionFilterCallback.
object ReturnExceptionHandler(object bindingExpression, Exception exception)
{
return "This is from the UpdateSourceExceptionFilterCallBack.";
}
The UpdateSourceExceptionFilterCallback can also return nullNothingnullptra null reference (Nothing in Visual Basic), the exception itself, or a ValidationError. For more information, see UpdateSourceExceptionFilterCallback.
For the complete sample, see Binding Validation Sample.