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