# HG changeset patch # User Steven Hollidge # Date 1334839162 -3600 # Node ID cd9faa9f024bc93c445ef4d6618eb194ecb865a3 # Parent f5c13b2b0149def032a29c358e0f9fe6e7538401 Working with INotifyDataErrorInfo diff -r f5c13b2b0149 -r cd9faa9f024b SilverlightValidation/SilverlightValidation/Properties/AssemblyInfo.cs --- a/SilverlightValidation/SilverlightValidation/Properties/AssemblyInfo.cs Thu Apr 19 12:07:54 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/Properties/AssemblyInfo.cs Thu Apr 19 13:39:22 2012 +0100 @@ -32,4 +32,4 @@ // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff -r f5c13b2b0149 -r cd9faa9f024b SilverlightValidation/SilverlightValidation/UserModel.cs --- a/SilverlightValidation/SilverlightValidation/UserModel.cs Thu Apr 19 12:07:54 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/UserModel.cs Thu Apr 19 13:39:22 2012 +0100 @@ -3,6 +3,15 @@ namespace SilverlightValidation { + public interface IUserModel + { + string Username { get; set; } + string Email { get; set; } + string Password { get; set; } + DateTime? DateOfBirth { get; set; } + string Description { get; set; } + } + public interface ICloneable { T Clone(); diff -r f5c13b2b0149 -r cd9faa9f024b SilverlightValidation/SilverlightValidation/UserModelValidator.cs --- a/SilverlightValidation/SilverlightValidation/UserModelValidator.cs Thu Apr 19 12:07:54 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/UserModelValidator.cs Thu Apr 19 13:39:22 2012 +0100 @@ -13,7 +13,7 @@ RuleFor(x => x.Password) .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$") - .WithMessage("Must contain at least one lower case, one upper case and a numeric character."); + .WithMessage("Must contain lower, upper and numeric chars."); RuleFor(x => x.Email) .EmailAddress() diff -r f5c13b2b0149 -r cd9faa9f024b SilverlightValidation/SilverlightValidation/UserView.xaml --- a/SilverlightValidation/SilverlightValidation/UserView.xaml Thu Apr 19 12:07:54 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/UserView.xaml Thu Apr 19 13:39:22 2012 +0100 @@ -6,7 +6,7 @@ xmlns:p="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" - d:DesignHeight="350" + d:DesignHeight="400" d:DesignWidth="450" mc:Ignorable="d"> @@ -42,7 +42,7 @@ Style="{StaticResource TextBoxStyle}" Text="{Binding Username, Mode=TwoWay, - ValidatesOnDataErrors=True, + ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" /> @@ -84,7 +84,7 @@ Style="{StaticResource TextBoxStyle}" Text="{Binding Email, Mode=TwoWay, - ValidatesOnDataErrors=True, + ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" /> - - - _errors = new Dictionary(); + #region INotifyDataErrorInfo methods and helpers - public virtual bool HasErrors - { - get { return _errors.Count > 0; } - } + private readonly Dictionary> _errors = new Dictionary>(); public void SetError(string propertyName, string errorMessage) { - _errors[propertyName] = errorMessage; - RaisePropertyChanged(propertyName); + if (!_errors.ContainsKey(propertyName)) + _errors.Add(propertyName, new List {errorMessage}); + + RaiseErrorsChanged(propertyName); } protected void ClearError(string propertyName) { - _errors.Remove(propertyName); - RaisePropertyChanged(propertyName); + if (_errors.ContainsKey(propertyName)) + _errors.Remove(propertyName); + + RaiseErrorsChanged(propertyName); } protected void ClearAllErrors() { - var properties = _errors.Select(error => error.Key).ToList(); + var errors = _errors.Select(error => error.Key).ToList(); - _errors.Clear(); + foreach (var propertyName in errors) + { + _errors.Remove(propertyName); + RaiseErrorsChanged(propertyName); + } + } + + #endregion - foreach (string property in properties) - RaisePropertyChanged(property); + public void RaiseErrorsChanged(string propertyName) + { + ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName)); } - public string Error - { - get - { - var sb = new StringBuilder(); + public event EventHandler ErrorsChanged = delegate { }; - foreach (var item in _errors) - { - sb.AppendLine(item.Value); - } - - return sb.ToString(); - } + public IEnumerable GetErrors(string propertyName) + { + return _errors.ContainsKey(propertyName) + ? _errors[propertyName] + : null; } - public string this[string columnName] + public bool HasErrors { - get { return _errors.ContainsKey(columnName) ? _errors[columnName] : string.Empty; } + get { return _errors.Count > 0; } } - - #endregion } }