[转] WPF – Editing Mode with Save and Cancel Capability
2013-01-23 11:06
232 查看
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 -
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.
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.
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.
To illustrate the concept, consider the following interface -
![]() | When a user clicks ‘Edit’:![]() |
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.
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.
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.
相关文章推荐
- WPF. SIMPLE ADORNER USAGE WITH DRAG AND RESIZE OPERATIONS
- GNU Radio: Synchronization and MIMO Capability with USRP Devices
- Install Nagios 3 with PNP4Nagios 0.6 in Bulk Mode with NPCD and npcdmod
- Save and render a webpage with phantomjs and node.js
- Howto: receive mail and save attachment with fetchmail, procmail and metamail
- IDs associated with each process and file’s mode word
- PHP 5.2.12/5.3.1 session.save_path safe_mode and open_basedir bypass
- 【iOS】Swift3:执行save()的时候出现:Call can throw, but it is not marked with 'try' and the error is not handl
- SELECT list is not in GROUP BY clause and contains nonaggregated column this is incompatible with sql_mode=only_full_group_by
- WPF Chart Control With Pan, Zoom and More
- 电子书下载:Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel
- [WPF] WPF User Controller with rotation and animation
- The difference between text mode and binary mode with file streams
- Struts 2 File Upload and Save Tutorial with Example
- How To Save Your App Data With NSCoding and NSFileManager
- Sublime-text markdown with Vim mode and auto preview
- 2D FDTD TE mode with a plane wave source and a PML ABC
- XAML with custom namespaces and prefixes (WPF/Silverlight)
- How to update WPF browser application manifest and xbap file with ‘mage.exe’
- tomcat runing on daemon with apr and ssl mode