Mercurial > silverbladetech
comparison delete me/SilverlightValidation/ViewModels/ViewModelBase.cs @ 59:3591c26bd63e
MVVMLight added
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sat, 21 Apr 2012 19:20:28 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
58:241e2f22ed3c | 59:3591c26bd63e |
---|---|
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 } |