這是我找到的解決方案。它使得INotifyDataErrorInfo在ViewModel中正確運行(當存在任何驗證錯誤時 - HasError爲真),並允許從viewModel添加驗證錯誤。除此之外,它不需要更改視圖,更改綁定或轉換器。
該解決方案包括:
- 添加自定義的驗證規則。
- 添加一個基本用戶控件(所有視圖都必須從中導出)。
- 在ViewModel庫中添加一些代碼。
添加自定義的驗證規則 - 校驗實體來完成實際的驗證和提升,當確認修改的事件:
class ValidationEntity : ValidationRule
{
public string Key { get; set; }
public string BaseName = "Base";
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var fullPropertyName = BaseName + "." + Key;
ValidationEntry entry;
var validationResult = new ValidationResult(true, null);
if ((entry = ValidationManager.Instance.FindValidation(fullPropertyName)) != null)
{
int errorNumber;
string errorString;
var strValue = (value != null) ? value.ToString() : string.Empty;
if (entry.Validate(strValue, out errorNumber, out errorString) == false)
{
validationResult = new ValidationResult(false, errorString);
}
}
if (OnValidationChanged != null)
{
OnValidationChanged(Key, validationResult);
}
return validationResult;
}
public event Action<string, ValidationResult> OnValidationChanged;
}
添加其保持活躍textboxs列表的基本用戶控件,添加驗證規則到每個文本框的結合: 這是在用戶控制基站的代碼:
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_textBoxes = FindAllTextBoxs(this);
var vm = DataContext as ViewModelBase;
if (vm != null) vm.UpdateAllValidationsEvent += OnUpdateAllValidationsEvent;
foreach (var textbox in _textBoxes)
{
var binding = BindingOperations.GetBinding(textbox, TextBox.TextProperty);
if (binding != null)
{
var property = binding.Path.Path;
var validationEntity = new ValidationEntity {Key = property};
binding.ValidationRules.Add(validationEntity);
validationEntity.ValidationChanged += OnValidationChanged;
}
}
}
private List<TextBox> FindAllTextBoxs(DependencyObject fe)
{
return FindChildren<TextBox>(fe);
}
private List<T> FindChildren<T>(DependencyObject dependencyObject)
where T : DependencyObject
{
var items = new List<T>();
if (dependencyObject is T)
{
items.Add(dependencyObject as T);
return items;
}
var count = VisualTreeHelper.GetChildrenCount(dependencyObject);
for (var i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(dependencyObject, i);
var children = FindChildren<T>(child);
items.AddRange(children);
}
return items;
}
當ValidationChange事件發生 - 該視圖被調用以被通知關於驗證錯誤:
private void OnValidationChanged(string propertyName, ValidationResult validationResult)
{
var vm = DataContext as ViewModelBase;
if (vm != null)
{
if (validationResult.IsValid)
{
vm.ClearValidationErrorFromView(propertyName);
}
else
{
vm.AddValidationErrorFromView(propertyName, validationResult.ErrorContent as string);
}
}
}
視圖模型基部保持兩個列表:其中使用由INotifyDataErrorInfo接口來顯示驗證錯誤
- _notifyvalidationErrors。
- _privateValidationErrors用於將驗證規則生成的錯誤顯示給用戶。
當從視圖添加驗證錯誤 - _notifyvalidationErrors更新爲空值(僅表示存在驗證錯誤)時,錯誤字符串不會添加到_notifyvalidationErrors。如果我們將它添加到那裏,我們將在文本框ErrorContent中兩次獲取驗證錯誤字符串。 驗證錯誤字符串也被添加到_privateValidationErrors(因爲我們希望能夠保持它在視圖模型) 這是在視圖模型的基礎代碼:
視圖中的INotifyDataErrorInfo實現:
public bool HasErrors
{
get { return _notifyvalidationErrors.Any(kv => kv.Value != null); }
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public void RaiseErrorsChanged(string propertyName)
{
var handler = ErrorsChanged;
if (handler != null)
handler(this, new DataErrorsChangedEventArgs(propertyName));
}
public IEnumerable GetErrors(string propertyName)
{
List<string> errorsForProperty;
_notifyvalidationErrors.TryGetValue(propertyName, out errorsForProperty);
return errorsForProperty;
}
用戶可以通過調用ViewModelBase AddValidationError和ClearValidationError方法從視圖中添加驗證錯誤。
public void AddValidationError(string errorString, [CallerMemberName] string propertyName = null)
{
_notifyvalidationErrors[propertyName] = new List<string>{ errorString };
RaiseErrorsChanged(propertyName);
}
public void ClearValidationError([CallerMemberName] string propertyName = null)
{
if (_notifyvalidationErrors.ContainsKey(propertyName))
{
_notifyvalidationErrors.Remove(propertyName);
RaiseErrorsChanged(propertyName);
}
}
視圖可以通過調用GetValidationErrors和GetValidationErrorsString方法得到視圖模型基地目前所有的驗證錯誤的列表。
public List<string> GetValidationErrors()
{
var errors = new List<string>();
foreach (var key in _notifyvalidationErrors.Keys)
{
errors.AddRange(_notifyvalidationErrors[key]);
if (_privateValidationErrors.ContainsKey(key))
{
errors.AddRange(_privateValidationErrors[key]);
}
}
return errors;
}
public string GetValidationErrorsString()
{
var errors = GetValidationErrors();
var sb = new StringBuilder();
foreach (var error in errors)
{
sb.Append("● ");
sb.AppendLine(error);
}
return sb.ToString();
}
我已經嘗試過了: <綁定路徑= 「SomeProperty」 ValidatesOnExceptions = 「真」/> 遺憾的是它不工作 –
您是收到異常? 發佈SomeProperty和引發異常的代碼。 –
財產: 私人詮釋_someProperty; public int SomeProperty { get {return _someProperty; } set { _someProperty = value; OnValidate(value); } } –