comparison SilverlightValidation/SilverlightValidation.PL/ViewModels/ViewModelBase.cs @ 96:188f8b366e87

Unit test project correctly setup as normal class library DLL.
author stevenhollidge <stevenhollidge@hotmail.com>
date Sat, 05 May 2012 13:29:56 +0100
parents SilverlightValidation/SilverlightValidation/ViewModels/ViewModelBase.cs@241e2f22ed3c
children
comparison
equal deleted inserted replaced
95:64e9903703a3 96:188f8b366e87
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Linq;
6
7 namespace SilverlightValidation.ViewModels
8 {
9 public class ViewModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
10 {
11 #region INotifyPropertyChanged method plus event
12
13 public event PropertyChangedEventHandler PropertyChanged = delegate { };
14
15 protected void RaisePropertyChanged(string propertyName)
16 {
17 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
18 }
19
20 #endregion
21
22 #region INotifyDataErrorInfo methods and helpers
23
24 private readonly Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();
25
26 public void SetError(string propertyName, string errorMessage)
27 {
28 if (!_errors.ContainsKey(propertyName))
29 _errors.Add(propertyName, new List<string> { errorMessage });
30
31 RaiseErrorsChanged(propertyName);
32 }
33
34 protected void ClearError(string propertyName)
35 {
36 if (_errors.ContainsKey(propertyName))
37 {
38 _errors.Remove(propertyName);
39 RaiseErrorsChanged(propertyName);
40 }
41 }
42
43 protected void ClearAllErrors()
44 {
45 var errors = _errors.Select(error => error.Key).ToList();
46
47 foreach (var propertyName in errors)
48 ClearError(propertyName);
49 }
50
51 public void RaiseErrorsChanged(string propertyName)
52 {
53 ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
54 }
55
56 public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged = delegate { };
57
58 public IEnumerable GetErrors(string propertyName)
59 {
60 if (propertyName == null) return null;
61 return _errors.ContainsKey(propertyName)
62 ? _errors[propertyName]
63 : null;
64 }
65
66 public bool HasErrors
67 {
68 get { return _errors.Count > 0; }
69 }
70
71 #endregion
72 }
73 }