如何让 Silverlight 数据绑定在用户键入时更新模型?
我目前正在使用 Silverlight 4 并遵循 MVVM 模式。我有一个绑定到我的 ViewModel 的登录框,如下所示:
<PasswordBox Password="{Binding Path=Password, Mode=TwoWay}" />
然后我有一个按钮绑定到一个命令,该命令侦听 ViewModel 的 PropertyChanged 事件,并且当其中一个数据绑定更新其数据时,它会执行检查现在是否有足够的数据来启用登录按钮。
但是,PropertyChanged 事件仅在用户从其中一个控件更改焦点时触发,我希望每次击键时都会更新模型,以便登录按钮尽快启用。
I'm currently using Silverlight 4 and following the MVVM pattern. I have login boxes bound to my ViewModel like so:
<PasswordBox Password="{Binding Path=Password, Mode=TwoWay}" />
I then later on have a button bound to a Command which listens to the ViewModel's PropertyChanged event and when one of the databindings has its data updated, it does a check to see if there is now enough data to enable the Login button.
However, the PropertyChanged event only fires when the user changes focus from one of the controls, I'd like the model to be updated with every keystroke so that the login button enables as soon as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个行为:
并修改您的 xaml:
现在,属性“密码”将随着用户类型而更新。
Create a behavior:
And modify your xaml:
Now the property Password will update as user types.
我建议使用一种行为来侦听 PasswordBox 的 OnKeyDown 事件并从那里触发 ViewModel 的事件(或运行您想要附加到 PropertyChanged 事件的其他一些自定义代码)。 TextBox 及其派生项(如 PasswordBox)的数据绑定在失去焦点之前不会更新,因此您必须手动更新绑定。
I'd recommend using a behavior that listens for the PasswordBox's OnKeyDown event and fires your ViewModel's event from there (or runs some other piece of custom code that you wanted to attach to the PropertyChanged event). Databinding for TextBoxes and their derivitives (like PasswordBox) don't update until they lose focus, so you have to manually update the binding.