MVVM - implementing 'IsDirty' functionality to a ModelView in order to save data

前端 未结 7 1449
攒了一身酷
攒了一身酷 2021-02-04 07:13

Being new to WPF & MVVM I struggling with some basic functionality.

Let me first explain what I am after, and then attach some example code...

I have a scree

7条回答
  •  孤城傲影
    2021-02-04 07:58

    Since your UserSave command is in the ViewModel, I would do the tracking of the "dirty" state there. I would databind to the selected item in the ListBox, and when it changes, store a snapshot of the current values of the selected user's properties. Then you can compare to this to determine if the command should be enabled/disabled.

    However, since you are binding directly to the model, you need some way to find out if something changed. Either you also implement INotifyPropertyChanged in the model, or wrap the properties in a ViewModel.

    Note that when the CanExecute of the command changes, you may need to fire CommandManager.InvalidateRequerySuggested().

提交回复
热议问题