annotate SilverlightGlimpse/SilverlightValidation/ViewModels/ViewModelBase.cs @ 82:352ecaa4fcfd

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