简体   繁体   中英

WPF Data Binding - Handling User Cancelation

So I have an app that makes heavy use of data binding. One feature in the app is a pop-up window that lets users manage some records. They select a record from a ListBox, and then they use a form to edit the properties of that record's object instance.

Users are typically accustomed to having the option to either Save or Cancel at the end of the form. Save commits the changes, and Cancel abandons the changes.

However, with the two-way data binding, the object is being updated in real-time as the field value changes.

Is there a "best practices" way of approaching the Commit/Cancel behavior with data binding within WPF? I've thought about cloning the object being edited so that the changes occur on that clone, and then the Save applies the changes to the original record, but I feel like this is something that Microsoft probably has built in already and I'm just unaware of how to use it properly.

WPF provide BindingGroup to deals with the situation you have run into. BindingGroup allow you to handle multiple value change at the same time. You can commit or cancel changes base on the result of validating all the changes at the same time but not just each one separately. And for edit transaction to work, the source(s) in BindingGroup should implement IEditableObject interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM