|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace MVVMDemo.Model
{
public class Employee : IDataErrorInfo, INotifyPropertyChanged
{
#region Constructor
/// <summary>
/// Constructor for Employee class with default parameters
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
/// <param name="age"></param>
/// <param name="notes"></param>
public Employee(string id = "", string name = "", uint age = 0, string notes = "")
{
_id = id;
_name = name;
_age = age;
_notes = notes;
}
#endregion
#region Properties
private string _id = string.Empty;
public string ID
{
get { return _id; }
set
{
_id = value;
RaisePropertyChanged("ID");
}
}
private string _name = string.Empty;
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
private uint _age = 0;
public uint Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged("Age");
}
}
private string _notes = string.Empty;
public string Notes
{
get { return _notes; }
set
{
_notes = value;
RaisePropertyChanged("Notes");
}
}
#endregion
#region IDataErrorInfo
public string Error
{
get
{
return this[string.Empty];
}
}
public string this[string propertyName]
{
get
{
string result = string.Empty;
propertyName = propertyName ?? string.Empty;
if (propertyName == string.Empty || propertyName == "ID")
{
if (string.IsNullOrEmpty(this.ID))
{
result = "Employee ID is invalid. ID cannot be null or blank";
}
else if (System.Text.RegularExpressions.Regex.IsMatch(this.ID, "[/!@#?/}[}{ ]"))
{
result = "Employee ID is invalid. ID cannot have special characters";
}
}
else if (propertyName == "Name")
{
if (string.IsNullOrEmpty(this.Name))
{
result = "Name is invalid. ID cannot be null or blank";
}
}
else if (propertyName == "Age")
{
if (Age > 150 || Age < 0)
{
result = "Age is invalid. Age should be between 0 and 150";
}
}
return result;
}
}
#endregion
#region INotifyPropertyChanged
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.