What's the best way to prevent losing TextBox focus when there is a validation error?

前端 未结 3 408
不思量自难忘°
不思量自难忘° 2021-02-04 20:44

I\'ve messed around with PreviewLostKeyboardFocus which almost gets you there. I\'ve seen a couple of implementations using LostFocus, but that just f

3条回答
  •  无人共我
    2021-02-04 21:09

    In my opinion, the best way is generally not to do it. It is almost always better to just disable the other controls or prevent saving until the value is valid.

    But if your design really needs this ability, here is what you should do:

    1. Intercept the Preview version of keyboard and mouse events at your window level, or whatever scope you want to prevent focus changes within (eg maybe not your menu bar).

    2. When the Tab KeyDown or Return KeyDown is detected in the text box, or when a MouseDown is detected outside the text box while it has the focus, call UpdateSource() on the binding expression, then if the validation has failed set Handled=true to prevent the KeyDown or MouseDown event from being processed further.

    3. Also continue handling PreviewLostKeyboardFocus to catch any causes of focus change that aren't from the keyboard or mouse, or that your other code didn't recognize.

提交回复
热议问题