1
votes

I have a WPF page (as my View) in an MVVM model. The View is an entry form with many textboxes. I have a custom ValidationRule built to validate each textbox and display tooltip warnings accordingly. However, I only want the "Commit" button to be enabled when all the validators pass. Right now, my "Commit" button's IsEnabled is bound to the DataContext based on other criteria. How do I add the caveat of "only be enabled when all validators pass" when my IsEnabled is already bound like so?

IsEnabled="{Binding IsDataLoaded}"
3

3 Answers

4
votes

Your Commit button should be bound to a RelayCommand in your ViewModel, and just set the CanExecute() to only be true if this.IsDataLoaded and this.IsValid

CommitCommand = new RelayCommand(
    param => SaveChanges()
    param => this.IsDataLoaded && this.IsValid
);

For verifying if the ViewModel is valid or not, I would suggest using IDataErrorInfo

5
votes

Assuming that you bind the Button to a Command, make the Command implement CanExecute so it only returns true when the validations rules validate.

0
votes

You can use MultiBinding with converter you want