WPF loses focus event to regain focus

Source: Internet
Author: User

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

 
  1. Private void textbox3_lostfocus (Object sender, routedeventargs E)
  2. {
  3. If (textbox3.text! = "ABC ")
  4. {
  5. This. Dispatcher. begininvoke (system. Windows. Threading. dispatcherpriority. Render,
  6. New action () =>
  7. {
  8. Textbox3.focus ();
  9. }));
  10. }
  11. }

Method 2: Use the lostkeyboardfocus method:[CSHARP]View
Plaincopy

 
  1. Private void textbox3_lostkeyboardfocus (Object sender, keyboardfocuschangedeventargs E)
  2. {
  3. If (textbox3.text! = "ABC ")
  4. {
  5. Textbox3.focus ();
  6. }
  7. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.