winform + INotifyPropertyChanged + IDataErrorInfo + ErrorProvider实现自动验证功能

一个简单的Demo.金山快盘下载链接:http://www.kuaipan.cn/file/id_226427209806521505.htm?source=1

话不多说,上代码。

1.实体类定义:

winform + INotifyPropertyChanged + IDataErrorInfo + 
ErrorProvider实现自动验证功能
class Student : INotifyPropertyChanged, IDataErrorInfo
    {
        // 用于保存验证错误信息。key 保存所验证的字段名称;value 保存对应的字段的验证错误信息列表
        private Dictionary<String, List<String>> errors = new Dictionary<string, List<string>>();

        private const string NAME_ERROR = "name 不能包含空格";
        private const string ID_ERROR = "id 不能小于 10";

        private int age;

        public int Age
        {
            get { return age; }
            set
            {
                if (IsIdValid(value))
                    age = value;
                else
                    age = 0;
                OnPropertyChanged("Age");
            }
        }



        private string stuName;
        public string StuName
        {
            get { return stuName; }
            set
            {
                IsNameValid(value);
                stuName = value;
                OnPropertyChanged("StuName");
            }
        }

        public bool IsIdValid(int value)
        {
            bool isValid = true;

            if (value < 10)
            {
                AddError("Age", ID_ERROR);
                isValid = false;
            }
            else
            {
                RemoveError("Age", ID_ERROR);
            }

            return isValid;
        }

        public bool IsNameValid(string value)
        {
            bool isValid = true;

            if (String.IsNullOrEmpty(value))
            {
                AddError("StuName", NAME_ERROR);
                isValid = false;
            }
            else
            {
                RemoveError("StuName", NAME_ERROR);
            }

            return isValid;
        }


        public void AddError(string propertyName, string error)
        {
            if (!errors.ContainsKey(propertyName))
                errors[propertyName] = new List<string>();

            if (!errors[propertyName].Contains(error))
                errors[propertyName].Add(error);
        }

        public void RemoveError(string propertyName, string error)
        {
            if (errors.ContainsKey(propertyName) && errors[propertyName].Contains(error))
            {
                errors[propertyName].Remove(error);

                if (errors[propertyName].Count == 0)
                    errors.Remove(propertyName);
            }
        }

        public string Error
        {
            get { return errors.Count > 0 ? "有验证错误" : "没有验证错误"; }
        }

        public string this[string propertyName]
        {
            get
            {
                if (errors.ContainsKey(propertyName))
                    return string.Join(Environment.NewLine, errors[propertyName]);
                else
                    return null;
            }
        }

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
Student.cs

2.界面Load绑定:

  注意绑定方式。

winform + INotifyPropertyChanged + IDataErrorInfo + 
ErrorProvider实现自动验证功能
 public partial class Form1 : Form
    {
        Student stu = new Student() { StuName = "", Age = 0 };
        public Form1()
        {
            InitializeComponent();
            this.textBox1.DataBindings.Add(new Binding("Text", stu, "StuName", false, DataSourceUpdateMode.OnValidation));
            this.textBox2.DataBindings.Add(new Binding("Text", stu, "Age", false, DataSourceUpdateMode.OnValidation));
            this.errorProvider1.DataSource = stu;
        }

    }
winform + INotifyPropertyChanged + IDataErrorInfo + 
ErrorProvider实现自动验证功能

winform + INotifyPropertyChanged + IDataErrorInfo + ErrorProvider实现自动验证功能,布布扣,bubuko.com

winform + INotifyPropertyChanged + IDataErrorInfo + ErrorProvider实现自动验证功能

上一篇:Illustrator(AI)设计绘制艺术感很强的剪纸实例教程


下一篇:win7下安装ubuntu12.04 并安装相应软件