tags:

views:

132

answers:

2

I have an application where you can open many different items (like in excel you can open many different tabs) If an item or "tab" is modified I want to detect the changes and allow the use to save or prompt if the user closes without saving. Maybe enable/disabling save button when a save is available/not available. I am using c# WPF with MVVM pattern.

+1  A: 

You may create a property in your view model class, that indicates whether the view model has been modified since its creation. The property could be of type bool and could be called IsDirty. This property should be set to true, when a property of your view model is modified. You can define this behaviour in your properties' set-method. When the user wants to close the GUI check if the IsDirty property is true, and save the changes.
Bind to the IsDirty property to have the enabled/disabled save-button.

Simon
+4  A: 

The common pattern for this problem is the 'isDirty' pattern. Basically you have a boolean flag 'isDirty' for all of your data pieces to mark whether or not they have been changed since the last save. You update this field when the data is modified or the document is saved.

Here is an example of a WPF 'isDirty' implementation: Almost-automatic INotifyPropertyChanged, automatic IsDirty, and automatic ChangeTracking

Ben Collier