changeset 54:cd9faa9f024b

Working with INotifyDataErrorInfo
author Steven Hollidge <stevenhollidge@hotmail.com>
date Thu, 19 Apr 2012 13:39:22 +0100
parents f5c13b2b0149
children f45b8551e79e
files SilverlightValidation/SilverlightValidation/Properties/AssemblyInfo.cs SilverlightValidation/SilverlightValidation/UserModel.cs SilverlightValidation/SilverlightValidation/UserModelValidator.cs SilverlightValidation/SilverlightValidation/UserView.xaml SilverlightValidation/SilverlightValidation/UserViewModel.cs SilverlightValidation/SilverlightValidation/ViewModelBase.cs
diffstat 6 files changed, 50 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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>
     {
         T Clone();
--- 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()
--- 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}" />
 
         <sdk:DescriptionViewer Grid.Row="1"
@@ -62,7 +62,7 @@
                      LostFocus="tbPassword_LostFocus"
                      Password="{Binding Password,
                                         Mode=TwoWay,
-                                        ValidatesOnDataErrors=True,
+                                        ValidatesOnNotifyDataErrors=True,
                                         NotifyOnValidationError=True}"
                      Style="{StaticResource PasswordBoxStyle}" />
 
@@ -84,7 +84,7 @@
                  Style="{StaticResource TextBoxStyle}"
                  Text="{Binding Email,
                                 Mode=TwoWay,
-                                ValidatesOnDataErrors=True,
+                                ValidatesOnNotifyDataErrors=True,
                                 NotifyOnValidationError=True}" />
 
         <sdk:DescriptionViewer Grid.Row="3"
@@ -105,7 +105,7 @@
                         LostFocus="dpDateOfBirth_LostFocus"
                         SelectedDate="{Binding DateOfBirth,
                                                Mode=TwoWay,
-                                               ValidatesOnDataErrors=True,
+                                               ValidatesOnNotifyDataErrors=True,
                                                NotifyOnValidationError=True}"
                         Style="{StaticResource DatePickerStyle}" />
         <sdk:DescriptionViewer Grid.Row="4"
@@ -124,13 +124,6 @@
                  Grid.Column="2"
                  Style="{StaticResource TextBoxStyle}"
                  Text="{Binding Description}" />
-
-        <sdk:DescriptionViewer Grid.Row="5"
-                               Grid.Column="3"
-                               Width="20"
-                               Description="Optional"
-                               Target="{Binding ElementName=tbDescription}" />
-
         <StackPanel Grid.Row="6"
                     Grid.Column="2"
                     HorizontalAlignment="Right"
--- a/SilverlightValidation/SilverlightValidation/UserViewModel.cs	Thu Apr 19 12:07:54 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/UserViewModel.cs	Thu Apr 19 13:39:22 2012 +0100
@@ -7,15 +7,6 @@
 
 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 class UserViewModel : ViewModelBase, IUserModel, IChangeTracking
     {
         #region Fields
--- a/SilverlightValidation/SilverlightValidation/ViewModelBase.cs	Thu Apr 19 12:07:54 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/ViewModelBase.cs	Thu Apr 19 13:39:22 2012 +0100
@@ -1,11 +1,12 @@
-using System.Collections.Generic;
+using System;
+using System.Collections;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
-using System.Text;
 
 namespace SilverlightValidation
 {
-    public class ViewModelBase : INotifyPropertyChanged, IDataErrorInfo
+    public class ViewModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
     {
         #region INotifyPropertyChanged method plus event
 
@@ -18,57 +19,56 @@
 
         #endregion
 
-        #region IDataErrorInfo methods and helpers
-
-        private readonly Dictionary<string, string> _errors = new Dictionary<string, string>();
+        #region INotifyDataErrorInfo methods and helpers
 
-        public virtual bool HasErrors
-        {
-            get { return _errors.Count > 0; }
-        }
+        private readonly Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();
 
         public void SetError(string propertyName, string errorMessage)
         {
-            _errors[propertyName] = errorMessage;
-            RaisePropertyChanged(propertyName);
+            if (!_errors.ContainsKey(propertyName))
+                _errors.Add(propertyName, new List<string> {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<DataErrorsChangedEventArgs> 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
     }
 }