Save changes for one object in WPF [closed]

好久不见. 提交于 2019-12-24 18:43:55

问题


In my application I manage persons information, I have a listBox display list of persons and three buttons Add/Edit/Delete, When I select row and press edit button the application open a new window with person information and save to some database table (ModifyCheck) that this person modifying now if other user try to edit it. In this person window I have two button (Save/Cancel). all controls in this window binding with object property, When I make some modifying and press save I should remove the row from (ModifyCheck).

The Problem Is :

If I open the person window and modifying some information then press Cancel, I will update ModifyCheck table by using SaveChange. In this case all the information that I modifyting will updated although I pressed Cancel.

How Can I apply SaveChanges() for ModifyCheck only ?


回答1:


Do not bind your entity properties directly to controls. Create a view model with the same properties, bind them to controls. When user makes some changes, it will modify viewModel's properties, instead of your entity. In case when user presses Save button, you have to update entity's fields. If user presses Cancel, nothing will happen to entity.

For simplicity, imagine you have a WinForms application. Every TextBox control has a name. When user presses Save button, you will assign it's value to entity:

person.FirstName = textBoxFirstName.Text;
db.SaveChanges();

Otherwise you will close the form and nothing will change person



来源:https://stackoverflow.com/questions/45131582/save-changes-for-one-object-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!