Equivalent to a WinForms TextBox.Validating Object in WPF

In WinForms, I can handle the Validated event to do something after the user has changed the text in the TextBox. Unlike TextChanged, Validated did not work every time the character changed; it only starts after the user has been executed.

Is there anything in WPF that I can use to get the same result, an event that was fired only after the user made a text change?

+4
source share
4 answers

LostFocus will fire when the user moves from your text box to any other control.

+2
source

LostFocus is not equivalent to Validate. This creates a lot of problems when there are several text fields on one screen, and each text field has some logic written in Validate. In event validation, you can easily control focus, but not in LostFocus.

+2
source

There does not seem to be a solution of its own. The LostFocus event is a good idea. But when the user presses Enter, he wants the TextBox to check the change. So here is my suggestion: use the LostFocus event and the KeyDown event when the Enter key is pressed.

private void TextBox_LostFocus(object sender, RoutedEventArgs e) { // code to lauch after validation } private void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { // call the LostFocus event to validate the TextBox ((TextBox)sender).RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent)); } } 
+1
source

You can also try Binding.ValidationRules

Documented at: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx

Here is an article to get you started:

How to implement binding checks:

http://msdn.microsoft.com/en-us/library/ms753962.aspx

0
source

All Articles