Using validation rules to disable a button for multiple textboxes

Simon Ong 21 Reputation points
2020-11-23T03:06:57.667+00:00

Hi :
I have multiple textboxes that implement Validation Rules which will verify if entries are less than 99.9 and must not be charactrers.

       I know i have to tie it with "CanExecute" with the Button (Save Button for example).

But how do i put all the validation results of the textboxes in the CanExecution Method just to enable the Button (Save Button)?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,768 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-11-23T08:12:09.997+00:00

    You can use below code(Derive from the Microsoft document: WPF Apps With The Model-View-ViewModel Design Pattern) in your ViewModel for Save button. And you can download the Sample from here:

    public ICommand SaveCommand  
    {  
        get  
        {  
            if (_saveCommand == null)  
            {  
                _saveCommand = new RelayCommand(param => this.Save(),  
                    param => this.CanSave);  
            }  
            return _saveCommand;  
        }  
    }  
    public void Save()  
    {  
        if (!_customer.IsValid)  
            throw new InvalidOperationException("...");  
        if (this.IsNewCustomer)  
            _customerRepository.AddCustomer(_customer);  
        base.OnPropertyChanged("DisplayName");  
    }  
    bool IsNewCustomer  
    {  
        get  
        {  
            return !_customerRepository.ContainsCustomer(_customer);  
        }  
    }  
    bool CanSave  
    {  
        get  
        {  
            return String.IsNullOrEmpty(this.ValidateCustomerType()) &&   
                _customer.IsValid;  
        }  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.