Enable Disable save button during Validation using IDataErrorInfo

我的梦境 提交于 2019-12-04 03:40:25

The Josh Smith Way of doing this is to create the following methods in the Model:

static readonly string[] ValidatedProperties =
{
    "Foo",
    "Bar"
};

/// <summary>
/// Returns true if this object has no validation errors.
/// </summary>
public bool IsValid
{
    get
    {
        foreach (string property in ValidatedProperties)
        {

            if (GetValidationError(property) != null) // there is an error
                return false;
        }

        return true;
    }
}

private string GetValidationError(string propertyName)
{
    string error = null;

    switch (propertyName)
    {
        case "Foo":
            error = this.ValidateFoo();
            break;

        case "Bar":
            error = this.ValidateBar();
            break;

        default:
            error = null;
            throw new Exception("Unexpected property being validated on Service");
    }

    return error;
}

The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:

/// <summary>
/// Checks if all parameters on the Model are valid and ready to be saved
/// </summary>
protected bool CanSave
{
    get
    {
        return modelOfThisVM.IsValid;
    }
}

Finally, if you are using RelayCommand, you can set the predicate of the command to the CanSave property, and the View will automatically enable or disable the button. In the ViewModel:

/// <summary>
/// Saves changes Command
/// </summary>
public ICommand SaveCommand
{
    get
    {
        if (_saveCommand == null)
            _saveCommand = new RelayCommand(param => this.SaveChanges(), param => this.CanSave);

        return _saveCommand;
    }
}

And in the View:

<Button Content="Save" Command="{Binding Path=SaveCommand}"/>

And that's it!

PS: If you haven't read Josh Smith's article yet, it will change your life.

you can add add a boolean property CanSave and set it at the end of your valiation method. Bind the IsEnabled from your button to IsValid. Somthing like this:

public bool CanSave
{
  get{ return canSave; }
  set{ canSave = value; RaisePropertyChanged( "CanSave" ); }
}
private bool canSave;

public string this[string columnName]
{
  //....
  CanSave = Result == String.Empty;
}

//xaml
<Button IsEnabled={Binding Path=CanSave}>Save</Button>

Here is my way of doing it using a combination of IDataErrorInfo interface, ValidationErrors Dictionary, and MVVM-Light messaging system. Straight forward and works like charm:

Model Class

public Dictionary<string, string> ValidationErrors = new Dictionary<string, string>();

public string this[string columnName]
{
  get
   {
     // Remove Property error from ValidationErrors prior to any validation
     ValidationErrors.Remove(propertyName);
     //----------------------------------------
     string Result = null;
     if (columnName == "FirstName")
     {
        if (String.IsNullOrEmpty(FirstName))
        {
           // Add Property error to ValidationErrors Dic
           ValidationErrors[propertyName] = Result = "Please enter first name";
           //----------------------------------------
        }
     }
     else if (columnName == "LastName")
     {
        if (String.IsNullOrEmpty(LastName))
        {
          // Add Property error to ValidationErrors Dic
          ValidationErrors[propertyName] = Result = "Please enter last name";
          //----------------------------------------
        }
     }

    // Send MVVM-Light message and receive it in the Code Behind or VM
    Messenger.Default.Send<PersonInfoMsg>(new PersonInfoMsg());
    //----------------------------------------
    return Result;
  }
}

View Code Behind

 public partial class PersonInfoView : UserControl
  {
    public PersonInfoView()
    {
        InitializeComponent();
        Messenger.Default.Register<PersonInfoMsg>(this, OnErrorMsg);
    }

    private void OnErrorMsg(PersonInfoMsg)
    {
        // In case of DataGrid validation
        foreach (PersonInfoModel p in GridName.ItemsSource)
        {
            if (p.ValidationErrors.Count == 0)
                SaveBtn.IsEnabled = true;
            else
                SaveBtn.IsEnabled = false;
        }
     }
  }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!