在WPF中保存一个对象的更改 [英] Save changes for one object in WPF

查看:101
本文介绍了在WPF中保存一个对象的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我管理人员信息,我有一个显示人员列表的列表框和三个按钮添加/编辑/删除,当我选择行并按编辑按钮时,应用程序将打开一个包含人员信息的新窗口并保存到某个数据库如果其他用户尝试对其进行修改,则此人现在正在修改的表(ModifyCheck)。在此人窗口中,我有两个按钮(保存/取消)。该窗口中的所有控件均与对象属性绑定,当我进行一些修改并按Save时,应从(ModifyCheck)中删除该行。

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).

问题是:

如果打开人员窗口并修改某些信息,然后按取消,我将使用SaveChange更新ModifyCheck表。在这种情况下,尽管按了取消,我修改的所有信息也会更新。

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.

如何仅对ModifyCheck应用SaveChanges()?

How Can I apply SaveChanges() for ModifyCheck only ?

推荐答案

请勿将您的实体属性直接绑定到控件。创建具有相同属性的视图模型,并将其绑定到控件。用户进行某些更改时,它将修改viewModel的属性,而不是您的实体。如果用户按下保存按钮,则必须更新实体的字段。如果用户按下取消,则实体将不会发生任何事情。

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.

为简单起见,假设您有一个WinForms应用程序。每个TextBox控件都有一个名称。当用户按下保存按钮时,您会将其值分配给实体:

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

这篇关于在WPF中保存一个对象的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆