使用IDataErrorInfo在验证期间启用禁用保存按钮

Tar*_*run 9 c# wpf mvvm light

如何在使用时进行验证时禁用/启用按钮IDataErrorInfo

我正在使用MVVMGalaSoft light Framework.在我的Model类中,我实现IDataErrorInfo了显示错误消息.

public string this[string columnName]
{
    get
    {
        Result = null;
        if (columnName == "FirstName")
        {
            if (String.IsNullOrEmpty(FirstName))
            {
                Result = "Please enter first name";
            }
        }
        else if (columnName == "LastName")
        {
            if (String.IsNullOrEmpty(LastName))
            {
                Result = "Please enter last name";
            }
        }

        else if (columnName == "Address")
        {
            if (String.IsNullOrEmpty(Address))
            {
                Result = "Please enter Address";
            }
        }

        else if (columnName == "City")
        {
            if (String.IsNullOrEmpty(City))
            {
                Result = "Please enter city";
            }
        }

        else if (columnName == "State")
        {
            if (State == "Select")
            {
                Result = "Please select state";
            }
        }

        else if (columnName == "Zip")
        {
            if (String.IsNullOrEmpty(Zip))
            {
                Result = "Please enter zip";

            }
            else if (Zip.Length < 6)
            {
                Result = "Zip's length has to be at least 6 digits!";

            }
            else
            {
                bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");

                if (zipNumber == false)
                {
                    Result = "Please enter only digits in zip";


                }
            }
        }
        else if (columnName == "IsValid")
        {
            Result = true.ToString();
        }

        return Result;

    }
}
Run Code Online (Sandbox Code Playgroud)

截图:http://i.stack.imgur.com/kwEI8.jpg

如何禁用/启用保存按钮.请建议?

谢谢

Pie*_*ler 17

约什-史密斯的方式这样做的是建立在模型下面的方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

然后,ViewModel包含一个CanSave读取IsValid Model上属性的Property :

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

最后,如果您正在使用RelayCommand,则可以将命令的谓词设置为CanSave属性,View将自动启用或禁用该按钮.在ViewModel中:

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

        return _saveCommand;
    }
}
Run Code Online (Sandbox Code Playgroud)

并在视图中:

<Button Content="Save" Command="{Binding Path=SaveCommand}"/>
Run Code Online (Sandbox Code Playgroud)

就是这样!

PS:如果你还没有读过Josh Smith的文章,它将改变你的生活.


sti*_*ijn 8

你可以添加一个布尔属性CanSave并在你的valiation方法结束时设置它.将IsEnabled从按钮绑定到IsValid.像这样的东西:

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>
Run Code Online (Sandbox Code Playgroud)

  • 如果有多个验证,则不起作用...每个属性调用[string columnName].因此,如果prop1无效,并且prop2有效,则CanSave设置为true. (5认同)