1

我已经在我的模型上实现了 INotifyDataErrorInfo。但我似乎无法在需要时使用它。例如。它不应该在启动时或我打字时验证错误。仅当单击按钮(保存)时。

我目前在我的 XAML 中有这个:

<TextBox Text="{Binding Car.Model, ValidatesOnNotifyDataErrors=False, UpdateSourceTrigger=Explicit}"/>

在 SaveCommand 下的 ViewModel 中:

Car.Validate();
if (Car.HasErrors)
{
 return;
}
//else save 

我的模型如下所示:

 Public class Car:ValidateModelBase
 {
    private string _model;
    [Required (ErrorMessage ="This field is required")]
    public string Model
    {
        get { return _model; }
        set { _model= value; RaisePropertyChanged(); }
    }
 }

然后是我对 ValidateModelBase 的实现:

public class ValidateModelBase: INotifyDataErrorInfo, INotifyPropertyChanged
{
    private ConcurrentDictionary<string, List<string>> _errors =
        new ConcurrentDictionary<string, List<string>>();

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
        ValidateAsync();
    }

    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    public void OnErrorsChanged(string propertyName)
    {
        var handler = ErrorsChanged;
        if (handler != null)
            handler(this, new DataErrorsChangedEventArgs(propertyName));
    }

    public IEnumerable GetErrors(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
        {
            return null;
        }
        List<string> errorsForName;
        _errors.TryGetValue(propertyName, out errorsForName);
        return errorsForName;
    }

    public bool HasErrors
    {
        get { return _errors.Any(kv => kv.Value != null && kv.Value.Count > 0); }
    }

    public Task ValidateAsync()
    {
        return Task.Run(() => Validate());
    }

    private object _lock = new object();
    public void Validate()
    {
        lock (_lock)
        {
            var validationContext = new ValidationContext(this, null, null);
            var validationResults = new List<ValidationResult>();
            Validator.TryValidateObject(this, validationContext, validationResults, true);

            foreach (var kv in _errors.ToList())
            {
                if (validationResults.All(r => r.MemberNames.All(m => m != kv.Key)))
                {
                    List<string> outLi;
                    _errors.TryRemove(kv.Key, out outLi);
                    OnErrorsChanged(kv.Key);
                }
            }

            var q = from r in validationResults
                    from m in r.MemberNames
                    group r by m into g
                    select g;

            foreach (var prop in q)
            {
                var messages = prop.Select(r => r.ErrorMessage).ToList();

                if (_errors.ContainsKey(prop.Key))
                {
                    List<string> outLi;
                    _errors.TryRemove(prop.Key, out outLi);
                }
                _errors.TryAdd(prop.Key, messages);
                OnErrorsChanged(prop.Key);
            }
        }
    }
}

事情是它正常工作,但是当窗口打开时,文本框已经是红色的。我希望它在启动时忽略验证,仅在我单击保存时验证。单击保存时,它将验证并查看是否有任何错误。出现错误时,将设置验证(现在应标记为红色)并且窗口保持打开状态。我怎样才能做到这一点。

4

1 回答 1

0

如果您的模型应该只在保存时验证,我会让您的模型实现IEditableObject并确保BeginEdit在进行更改之前调用它,并EndEdit在提交这些更改(即保存)之前调用。

让您的基类跟踪它是否处于“编辑模式”,如果是,则禁止任何验证。当EndEdit被调用时,再次允许验证并触发ErrorsChanged。如何处理由您决定CancelEdit

某些控件(例如数据网格)具有内置支持,IEditableObject并且会BeginEdit在您开始编辑行和EndEdit提交行时调用。这是一个非常有用的界面。

于 2018-01-29T14:43:37.813 回答