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