Validating a TextBox as the User Types

The BindingExpression class’s UpdateSource method forces the target property value to be assigned to its source property. This allows you to perform input validation while the user is entering text into a TextBox, rather than after the TextBox loses focus.

I have created a class called UpdateSourceTriggerExtender, which contains an attached property that is used to trigger an update of the binding source whenever the TextBox’s text changes.

When the attached property is placed on a TextBox, the UpdateSourceTriggerExtender class’s HandleUpdatePropertyChanged method is called, which subscribes to the TextBox.TextChanged event (see Listing 26.1). When the text is changed the BindingExpression for the TextProperty ...

Get Windows® Phone 8 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.