Using a DataForm and an external Save button

222 Views Asked by At

I have a DataForm with AutoCommit = "False" and an external Save button bound to a Command SaveCommand.

If I want the Save command disabled when no changes to the data (I'm using a ViewModel) are pending, when do I have to execute SaveCommand.RaiseECanExecuteChanges()?

1

There are 1 best solutions below

0
On BEST ANSWER

I normally override RaisePropertyChanged and set my CanExecute predicate to whether the ViewModel is dirty or not.

class ViewModel : ViewModelBase
{
    public DelegateCommand SaveCommand { get; set; }
    private bool _isDirty;

    public ViewModel()
    {
        SaveCommand = new DelegateCommand(() => OnExecuteSave(), () => CanExecuteSave());
    }

    private void CanExecuteSave()
    {
        // do your saving
    }

    private bool CanExecuteSave()
    {
        return !_isDirty;
    }

    protected override void RaisePropertyChanged(string propertyName)
    {
        base.RaisePropertyChanged(propertyName);
        _isDirty == true;
        SaveCommand.RaiseCanExecuteChanged();
    }
}

Hope that helps.