zoukankan      html  css  js  c++  java
  • [转] WPF – Editing Mode with Save and Cancel Capability

    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 -

    image When 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.
  • 相关阅读:
    第一篇:白话tornado源码之一个脚本引发的血案
    第二篇:白话tornado源码之待请求阶段
    Python 面向对象(初级篇)
    python 面向对象(进阶篇)
    Csharp启动exe文件
    UNIX时间戳
    JS日期格式化代码
    Android之什么是Activity和常用的ADB命令以及Android项目结构的认识
    thread同步测试
    Fluent NHibernate使用小结:(1)通用配置文件创建方法
  • 原文地址:https://www.cnblogs.com/CSharpSPF/p/2872738.html
Copyright © 2011-2022 走看看