What is the best way to determine if my page needs to be saved - using WPF MVVM

I have an application in which you can open many different elements (for example, in excel you can open many different tabs). If the item or tab is changed, I want to detect the changes and allow the use for saving or querying if the user closes without saving. It is possible to enable / disable the save button in the presence / unavailability of saving. I am using C # WPF with the MVVM pattern.

+4
source share
2 answers

A common pattern for this problem is the "isDirty" pattern. Basically, you have a boolean flag "isDirty" for all your data to indicate if it has changed since the last save. This update is updated when data is changed or a document is saved.

Here is an example WPF implementation of 'isDirty': Almost automatic INotifyPropertyChanged function, automatic IsDirty and automatic track change

+5
source

You can create a property in the view model class that indicates whether the view model has changed since it was created. The property may be of type bool and may be called IsDirty . This property must be set to true when the property of your view model is changed. You can define this behavior in a given property method. When the user wants to close the GUI check if the IsDirty property is true and saves the changes.
Bind to the IsDirty property to enable / disable the save button.

+2
source

Source: https://habr.com/ru/post/1309774/


All Articles