annotate delete me/SilverlightValidation/ViewModels/ViewModelBase.cs @ 64:ba89e36631bc

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