forked from rockfordlhotka/DataErrorInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
59 lines (50 loc) · 1.87 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Library;
using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
namespace DataErrorInfo
{
class Program
{
static void Main(string[] args)
{
var person = new PersonEdit();
var idei = person as IDataErrorInfo;
var indei = person as INotifyDataErrorInfo;
indei.ErrorsChanged += (sender, e) => Console.WriteLine("### ErrorsChanged");
person.EnableINotifyDataErrorInfo = false;
Console.WriteLine("*** IDataErrorInfo ***");
Console.WriteLine("Should be error");
Console.WriteLine(idei.Error);
person.Name = "Fred";
Console.WriteLine("Should be null");
Console.WriteLine(idei.Error);
person.Name = "";
Console.WriteLine("Should be error");
Console.WriteLine(idei.Error);
person.EnableIDataErrorInfo = false;
person.EnableINotifyDataErrorInfo = true;
Console.WriteLine();
Console.WriteLine("*** INotifyDataErrorInfo ***");
Console.WriteLine("Should be error");
Console.WriteLine(GetErrors(indei.GetErrors("Name")));
person.Name = "Fred";
Console.WriteLine("Should be null");
Console.WriteLine(GetErrors(indei.GetErrors("Name")));
person.Name = "";
Console.WriteLine("Should be error");
Console.WriteLine(GetErrors(indei.GetErrors("Name")));
}
private static string GetErrors(IEnumerable errors)
{
var builder = new StringBuilder();
foreach (var error in errors)
builder.Append($"{error},");
if (builder.Length > 2)
return builder.ToString().Substring(0, builder.Length - 1);
else
return string.Empty;
}
}
}