文本更改时的密码框验证而不是失去焦点
框架:Silverlight 4
我有一个简单的ChildWindow
,用户名TextBox
和密码PasswordBox
。我已将事件处理程序附加到窗口的 KeyDown
事件。
private void onKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
cancelButtonClick(null, null);
if (e.Key == Key.Enter)
okButtonClick(null, null);
}
我想要实现的是,当用户按下键盘上的 Enter 键时,程序的行为就像用户单击了“确定”按钮一样。
问题是验证。
Silverlight 的PasswordBox 的默认行为是在控件失去焦点时执行验证。我的 PasswordBox
绑定到某个 User
对象。当我单击 Enter 按钮时,将调用事件处理程序,该处理程序又调用 okButtonClick(null, null)。问题在于,当时 PasswordBox
尚未失去焦点,因此 PasswordBox
所绑定的 user.Password
属性,仍然是空的。
我尝试将 btnOK.Focus()
放在 okButtonClick(null, null)
之前,但无济于事。
如何设置绑定,以便控件在每次文本更改时更新绑定,而不是在 LostFocus
事件时更新绑定?实现我需要的正确方法是什么?
Framework: Silverlight 4
I have a simple ChildWindow
with Username TextBox
and Password PasswordBox
. I have attached an event handler to the window's KeyDown
event.
private void onKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
cancelButtonClick(null, null);
if (e.Key == Key.Enter)
okButtonClick(null, null);
}
What I'm trying to achieve is that when the user presses Enter key on the keyboard, the program will behave as if the user had clicked the OK button.
The problem is the validation.
The default behavior for Silverlight's PasswordBox is to perform validation when the control loses focus. My PasswordBox
is bound to some User
object. When I click the Enter button, the event handler gets called, which in turn calls the okButtonClick(null, null)
. The problem is that the PasswordBox
has not yet lost the focus at that time, so the user.Password
property, which the PasswordBox
is bound to, is still empty.
I've tried to place btnOK.Focus()
before the okButtonClick(null, null)
but to no avail.
How to set the binding so that the control will update binding on every text change instead of on the LostFocus
event? What is the right way to achieve what I need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对
Passwordbox
使用PasswordChanged
事件。Textbox
有TextboxChanged
事件。Use
PasswordChanged
event forPasswordbox
.Textbox
hasTextboxChanged
event.我通过绊倒 根据此解决方案。
它基本上使用附加属性来订阅 TextChanged/PasswordChnaged 事件,然后在该事件中更新绑定源。调整后 满足我的需求的解决方案,这就是我得到的(并且它工作完美):
I have solved the problem by stumbling upon this solution.
It basicly uses attached property to subscribe to the TextChanged/PasswordChnaged events, and then in the event it updates the binding source. After adapting the solution to my needs, here is what I got (and it works flawlessly):