public string this[string columnName] { get { if (!IsValidateEnabled) { return(string.Empty); //未启用验证时不进行验证 add by ljc 2015-08-01 } if (ValidateHelper.GetValidator(this, _type) != null) { //先判断有没有FluentValidation错误,有的话直接返回FluentValidation错误(没有的话还按原先的方式验证)add by ljc 2015-07-29 var flentValidationErrors = ValidateHelper.GetFluentValidationErrors(this, columnName); if (!string.IsNullOrEmpty(flentValidationErrors)) { return(flentValidationErrors); } return(string.Empty);//如果用了FluentValidation,就不再用老的方式验证了 add by ljc 2015-08-03 } //当ViewModel中值发生变化是触发规则验证 var vc = new ValidationContext(this, null, null) { MemberName = columnName }; if (_propertyGetters.ContainsKey(columnName)) { //不想验证的属性就不进行验证 if (ExcludedValidatePropertyNames != null && ExcludedValidatePropertyNames.Contains(columnName)) { return(string.Empty); } //获取触发的属性值 object value = _propertyGetters[columnName].GetValue(this, null); //在_validators中找到相应的规则,并触发验证,返回错误信息 var errors = from v in _validators[columnName] select v.GetValidationResult(value, vc) into result where result != null select result.ErrorMessage; //更新Error属性 //OnPropertyChanged("Error");//这句不能加,否则会与Error属性的Get方法形成死循环 return(string.Join(Environment.NewLine, errors)); } //OnPropertyChanged("Error");//这句不能加,否则会与Error属性的Get方法形成死循环 return(string.Empty); } }