wpf:设置 DataContext 时触发 TextChanged 事件
我有一个带有单个文本框的简单视图,该文本框将数据绑定到带有单个字符串属性的简单 ViewModel。
我需要捕获该文本框的 TextChanged 事件,以便我可以执行一些验证魔法。
我遇到的问题是,当为视图设置 DataContext 时,会为该文本框触发 TextChanged 事件。
是否有一个标准机制可以用来确定事件是否由于设置了 DataContext 以及用户进行更改而触发?
谢谢!
I've got a simple View with a single textbox that gets databound to a simple ViewModel with a single string property.
I need to catch the TextChanged event of that textbox so that I can do a little validation magic.
The problem that I am running into is that the TextChanged event fires for that textbox when the DataContext is set for the View.
Is there a standard mechanism that I can use to determine if the event is firing because of the DataContext being set versus when the user is making changes?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知没有这样的机制。相反,您应该做的是使用 WPF 的标准方法来执行验证魔法。请参阅以下链接:http://msdn.microsoft.com/en -us/library/ms752347.aspx#data_validation。
不管怎样,只要你使用 MVVM,你总是可以检测到视图模型中绑定属性的 setter 中的文本发生了变化。
As far as I know there is no such mechanism. What you should do instead is to do your validation magic using standard means of WPF. Please see the following link: http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_validation.
Anyway, as long as you use MVVM you can always detect that text has changed in the setter of the bound property in your view model.