1

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()?

Rafael
  • 2,413
  • 4
  • 32
  • 54

1 Answers1

1

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.

Barracoder
  • 3,696
  • 2
  • 28
  • 31