使用IDataErrorInfo进行验证时,启用“禁用保存"按钮 [英] Enable Disable save button during Validation using IDataErrorInfo

查看:53
本文介绍了使用IDataErrorInfo进行验证时,启用“禁用保存"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to disable/enable a button while doing validation using IDataErrorInfo?

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

I am using MVVM using GalaSoft light Framework. In my Model class I have implemented IDataErrorInfo to display the error messages.

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;

    }
}

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

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

How to disable/enable save button. Kindly suggest?

谢谢

推荐答案

JoshSmith Way 的方法是在模型中创建以下方法:

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;
}

然后,ViewModel包含一个 CanSave 属性,该属性读取模型上的 IsValid 属性:

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;
    }
}

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

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;
    }
}

在视图中:

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

就是这样!

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

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

这篇关于使用IDataErrorInfo进行验证时,启用“禁用保存"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆