How to suppress validation when nothing is entered

前端 未结 8 1314
醉梦人生
醉梦人生 2020-12-13 04:40

I use WPF data binding with entities that implement IDataErrorInfo interface. In general my code looks like this:

Business entity:

p         


        
8条回答
  •  时光说笑
    2020-12-13 04:53

    You can change your person class to fire validation error only if Name property was ever changed:

    public class Person : IDataErrorInfo {
    
        private bool nameChanged = false;
        private string name;
        public string Name {
            get { return name; }
            set { 
                name = value;
                nameChanged = true;
            }
        }
    
    //... skipped some code
    
        string IDataErrorInfo.this[string columnName] {
            get {
                if(nameChanged && columnName == "Name" && string.IsNullOrEmpty(Name)) 
                    return "Name is not entered"; 
                return string.Empty;
            }
        }
    }
    

提交回复
热议问题