annotate SilverlightGlimpse/SilverlightValidation/ViewModels/ViewModelBase.cs @ 69:a0bcd783e612

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