В примерах параграфа будут использоваться классы Person и Repository.
// классы объявлены в пространстве имён Domain
public class Person: INotifyPropertyChanged, IDataErrorInfo
{
private string _name;
private double _age;
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value, "Name"); }
}
public double Age
{
get { return _age; }
set { SetProperty(ref _age, value, "Age"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public string this[string columnName]
{
get
{
switch (columnName)
{
case "Name":
return string.IsNullOrEmpty(Name)?
"Name cannot be empty": null;
case "Age":
return Age < 0? "Incorrect age": null;
}
return null;
}
}
public string Error
{
get { return null; }
}
private void SetProperty<T>(ref T field, T value, string name)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{
field = value;
var handler = PropertyChanged;
if (handler!= null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
public static class Repository
{
public static List<Person> ReadAll()
{
var trinity = new Person {Name = "Trinity", Age = 32};
var smith = new Person {Name = "Agent Smith", Age = 39};
var brown = new Person {Name = "Agent Brown", Age = 39};
var jones = new Person {Name = "Agent Jones", Age = 39};
var neo = new Person {Name = "Neo", Age = 37};
return new List<Person> {trinity, smith, brown, jones, neo};
}
}