-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidateInstance.cs
More file actions
43 lines (40 loc) · 1.24 KB
/
ValidateInstance.cs
File metadata and controls
43 lines (40 loc) · 1.24 KB
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
/// <summary>
/// Validates the current value of all properties.
/// </summary>
protected void ValidateInstance()
{
if (PropertyValidationSource != this)
throw new NotSupportedException("Instance validation is only supported if PropertyValidationInstance does not return another instance.");
HashSet<string> affectedPropertyNames = new HashSet<string>();
if (validationErrors != null)
{
foreach (string propertyName in validationErrors.Keys)
{
affectedPropertyNames.Add(propertyName);
}
validationErrors.Clear();
}
var results = new List<ValidationResult>();
var context = new ValidationContext(this);
if (!Validator.TryValidateObject(this, context, results, true))
{
if (validationErrors == null)
{
validationErrors = new Dictionary<string, ICollection<string>>();
}
foreach (var group in results.GroupBy(r => r.MemberNames.FirstOrDefault() ?? ""))
{
validationErrors.Add(group.Key, group.Select(r => r.ErrorMessage).ToList());
affectedPropertyNames.Add(group.Key);
}
}
if (!HasErrors)
{
validationErrors = null;
}
// Raise the ErrorsChanged event for all affected properties
foreach (var propertyName in affectedPropertyNames)
{
OnErrorsChanged(propertyName);
}
}