WPF detects data when textbox loses focus, and gets focus again when an error occurs. Solution
Using the focus () method directly in the lostfocus event of textbox in WPF will cause an endless loop.
There are two methods for correct use:
Method 1:
[CSHARP]View
Plaincopy
- Private void textbox3_lostfocus (Object sender, routedeventargs E)
- {
- If (textbox3.text! = "ABC ")
- {
- This. Dispatcher. begininvoke (system. Windows. Threading. dispatcherpriority. Render,
- New action () =>
- {
- Textbox3.focus ();
- }));
- }
- }
Method 2: Use the lostkeyboardfocus method:[CSHARP]View
Plaincopy
- Private void textbox3_lostkeyboardfocus (Object sender, keyboardfocuschangedeventargs E)
- {
- If (textbox3.text! = "ABC ")
- {
- Textbox3.focus ();
- }
- }
Note: you can find the following on msdn:
Lostkeyboardfocus
Occurs when the keyboard focus is no longer on this element
Lostfocus
This element occurs when the logical focus is lost.
Isfocused
Gets a value that determines whether the element has a logical focus.
Focus
Try to set the focus to this element
True if both the keyboard focus and logical focus are set to this element; false if only the logical focus is set to this element or the focus is not forcibly changed by this method call