1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ComponentModel;
5
6 namespace DataBindingTest
7 {
8 class Person : IDataErrorInfo
9 {
10 private Dictionary<string, string> errors = new Dictionary<string, string>();
11
12 private string error = string.Empty;
13
14 private string lastname;
15 private string firstname;
16
17 private void Validate(string field, string value)
18 {
19 switch (field)
20 {
21 case "Lastname":
22 if (String.IsNullOrEmpty(value))
23 {
24 this.errors["Lastname"] = "There is an error";
25 }
26 else
27 {
28 this.errors.Remove("Lastname");
29 }
30 break;
31 case "Firstname":
32 if (String.IsNullOrEmpty(value))
33 {
34 this.errors["Firstname"] = "There is an error";
35 }
36 else
37 {
38 this.errors.Remove("Firstname");
39 }
40 break;
41 default:
42 break;
43 }
44 }
45
46 public string Lastname
47 {
48 get
49 {
50 this.Validate("Lastname", this.lastname);
51 return this.lastname;
52 }
53
54 set
55 {
56 this.lastname = value;
57 this.Validate("Lastname", this.lastname);
58 }
59 }
60
61 public string Firstname
62 {
63 get
64 {
65 this.Validate("Firstname", this.firstname);
66 return this.firstname;
67 }
68
69 set
70 {
71 this.firstname = value;
72 this.Validate("Firstname", this.firstname);
73 }
74 }
75
76 #region IDataErrorInfo Members
77
78 public string Error
79 {
80 get
81 {
82 if (this.errors.Count > 0)
83 {
84 return "There are " + this.errors.Count + " errors";
85 }
86
87 return null;
88 }
89 }
90
91 public string this[string columnName]
92 {
93 get
94 {
95 if (this.errors.ContainsKey(columnName))
96 {
97 return this.errors[columnName];
98 }
99 else
100 {
101 return null;
102 }
103 }
104 }
105
106 #endregion
107 }
108 }