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

9

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

我正在使用 GalaSoft light 框架的 MVVM。在我的 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;

    }
}

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

如何禁用/启用保存按钮。请提供建议?

谢谢

3个回答

18

实现这一点的Josh Smith方法是在模型中创建以下方法:

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 属性,该属性读取 Model 上的 IsValid 属性:

/// <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属性,视图将自动启用或禁用该按钮。在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}"/>

这就是全部内容!

附注:如果你还没有读过Josh Smith的文章,那将会改变你的生活。


将验证逻辑放入您的模型中并不是一个好方法,因为验证逻辑可能会在不同情况下发生更改。 - Amir Oveisi
为了简单起见,它可以只是 public bool IsValid => ValidatedProperties.All(p => GetValidationError(p) == null); - Daniel Dušek
我的问题是,这种方法确实可以处理字符串错误,但无法处理 Decimal、long、double 等其他类型的错误。当用户输入“ABCDE...”时,在 datagrid 的单元格周围会出现红色边框,但在 C# 中没有任何指示表明发生了这个错误,是否有解决方案?把“price”设置为字符串不是一个好选择,因为它需要大量的转换来与 Quantity、TotalPrice、TAX 等相乘,这将破坏应用程序并使其更难管理... - RAMM-HDR

8

你可以添加一个布尔属性CanSave,并在验证方法的末尾设置它。将按钮的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>

如果您使用 IDataErrorInfo,那么这实际上比被接受的答案要好得多。如果您只是像我现在这样使用验证规则属性,那么您可能必须采用被接受的答案 :) - GONeale
5
如果有多个验证,代码就无法正常工作......对于每个属性,都会调用this[string columnName]。因此,如果prop1无效,而prop2有效,则CanSave将被设置为true。 - Peter van Kekem
@PetervanKekem 那部分在 //....:每次调用函数时,Result必须基于先前的结果重新计算。 - stijn
是的,这是可能的。然而,我认为这太冒险/复杂了。我选择实现@PieterMüller的解决方案。 - Peter van Kekem

1
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:

模型类

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

查看代码后台
 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;
        }
     }
  }

我面临的问题是,这种方法确实可以处理字符串错误,但不能处理十进制、长整型、双精度等类型。当用户输入“ABCDE...”时,在数据网格中单元格周围会出现红色边框,但在C#中没有任何指示表明发生了这个错误,是否有任何解决方案?将“价格”设置为字符串不是一个好选择,因为它需要大量的转换才能将其乘以数量、总价、税等,这将破坏应用程序并使其更难管理... - RAMM-HDR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接