c# - Interface IDataErrorInfo isn't working -
i have following class have implemented idataerrorinfo
interface doesn't work i.e doesn't validation. code seems perfect. dont know why. put break point , doesn't enter idataerrorinfo members
region.
product class
[datacontract()] public class product : idataerrorinfo { [datamember()] public string name{get;set;} [datamember()] public string code{get;set;} #region idataerrorinfo members public string error { { return null; } } public string this[string property] { { switch (property) { case "name": if (string.isnullorempty(name)) return "name required"; break; case "code": if (string.isnullorempty(code)) return "code required"; break; default: break; } return null; } } #endregion public product(string name, string code) { name = name; code = code; } }
xaml binding textbox
<textbox grid.column="1" horizontalalignment="left" height="23" margin="24,9,0,0" textwrapping="wrap" verticalalignment="top" width="148" x:name="txtname" text="{binding name,mode=twoway,validatesondataerrors=true}" maxlength="50"/>
you need make object observable using inotifypropertychanged
along idataerrorinfo
in order the binding know properties have changed , check if there errors when validatesondataerrors=true
public class product : idataerrorinfo, inotifypropertychanged { string _name; [datamember()] public string name{ { return _name; } set { _name = value; notifypropertychanged("name"); } } //...other code removed brevity public event propertychangedeventhandler propertychanged; protected void notifypropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } }
you move property changed functionality out base class reuse so
public abstract class propertychangedbase: inotifypropertychanged { public event propertychangedeventhandler propertychanged; protected void notifypropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } }
and use
public class product : propertychangedbase, idataerrorinfo { //code removed brevity }
Comments
Post a Comment