WPF 中的输入验证
我正在使用 MVVM 设计模式在 wpf 中开发应用程序。当选择一个项目时,我有一个列表框,然后打开一个对话框,在可编辑模式下具有相同的记录。该对话框与列表中选定的项目绑定。我已使用 IDataErrorInfo 应用文本框的验证规则。当用户更新对话框上的记录时,每次按键时,列表框中选定的记录也会更改。如果用户按下保存按钮,那么我会将更改提交到数据库。但如果用户单击“取消”按钮,那么我不会向数据库提交更改,但列表框会使用 GUI 中的当前更新进行更新。当我刷新列表时,旧值再次出现。我的要求是仅当用户点击保存按钮时更新列表框,而不是在对话框上的每个按键时更新列表框。我首先使用 linq to sql 类填充通用列表,然后将列表框与其绑定。请让我知道我必须做什么。
提前致谢
i am developing an application in wpf using MVVM design pattern. i have a listbox when an item is slected then a dialog is open having the same record in editable mode. this dialog is binded with the selected item of the list. i have apply the validation rule for textbox using IDataErrorInfo. when the user update a record on dialogbox then at every key press, the selected record in listbox is also changed. if the user press save button then i submit changes to database. but if user click cancel button then i do not submit changes to database but the list box is updated with the current updation in GUI. when i refresh the list then old value appears again. My requirement is to update the listbox only when the user hit the save button but not on every key press on dialog box. I first fill the generic list with the linq to sql classes then bind the listbox with it. Please let me know what i have to do.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在两种表单上编辑同一对象。您应该将 SelectedItem 传递到对话框表单,然后重新查询数据库以获取传递给构造函数的项目。这有两个作用:允许您在编辑对象时取消更改,并为用户提供数据库中的最新数据。
这样想...如果列表框包含的数据甚至是几分钟前的数据,您的用户将修改可能已经被运行您的应用程序的另一个用户更改的数据。
一旦用户在对话框中保存(或删除)记录,您必须刷新列表框。通常我使用以下方法:
DialogViewModel:
ListBox ViewModel
The problem is that you are editing the same object on both forms. You should pass the SelectedItem to the dialog form, but then re-query the database for the item that was passed to the constructor. This does two things: allows you to cancel the changes when the object has been edited, and provides the user with the most current data from the database.
Think of it this way... If the listbox contained data that was even a few minutes old, your user would be modifying data that may have already changed by another user running your application.
Once the user saves (or deletes) the record in the dialog form, you must then refresh the listbox. Typically I use the following method:
DialogViewModel:
ListBox ViewModel