[转] WPF – Editing Mode with Save and Cancel Capability

应用程序常需在视图模式展示实体,用户编辑时加载编辑视图。在MVVM应用里,用户点击编辑后,可使用同一视图模型,但存在保存和取消操作的问题。文中介绍两种处理方法,一是使用同一实例,二是用克隆和合并方法创建新实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A common scenario that is required by applications is the ability to show an entity in a view mode, and when the user wishes to edit that entity, another view is loaded in edit mode.   In most cases, you would allow the user to save or cancel his changes.

To illustrate the concept, consider the following interface -

imageWhen a user clicks ‘Edit’:        
image

 

Usually, in a MVVM application, you would have a view model for each list item on the left screenshot.   When the user clicks ‘Edit’ and the editable view is shown as modal dialog, you may want to use the same view model.

However, if it is the same instance, how would you support cancelling the save operation. Additionally, how would you prevent that changes in the edit mode won’t be reflected in the view mode until the user chooses to save the changes. (If you use bindings, the change would reflect in the view mode as well, as long as the binding is active and the source is updated).

Well, there are numerous ways to go about it.

    1. Use the same instance: To use the same actual instance, a common approach is to define all the bindings in the editable view with update source trigger set to ‘Explicit’.      This gives you the ability to save state explicitly when the user chooses to save his changes.      This method isn’t suitable for all the cases. If you have more than a plain simple view model where properties may set other properties or commands can be invoked from the edit view – you’re in a dead end. Plus, you can’t use the IDataErrorInfo validation model since the bindings don’t update the source nor calls its validation methods.     
    2. Use a separate instance using some sort of cloning and merging methods: To support more advanced scenarios, or simply provide a complete solution, you would generally prefer binding the editable view to a new instance.      Obviously, you would need to construct the new instance, either imperative instantiation/cloning or use serialization which is quite a common technique in this scenario.      Finally, if the user chooses to save his changes, you would need to merge back the changes to the instance in the view mode or simply replace it with the new instance.

转载于:https://www.cnblogs.com/CSharpSPF/archive/2013/01/23/2872738.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值