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

前端 未结 3 442
不思量自难忘°
不思量自难忘° 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:11

    To add onto Ray's answer:

    UpdateSource is called like so:

    BindingExpression be = userTextbox.GetBindingExpression(TextBox.TextProperty);
    be.UpdateSource();
    

    Also, as an alternative you can set the text box binding to:

    UpdateSourceTrigger = "PropertyChanged";
    

    The latter will cause a continuous check, whereas the former will check when needed (performant).

提交回复
热议问题