i achieving function "wpf validation on startdate , enddate (startdate less enddate)", write code in code behind throw exception if enddate less startdate, , works. met problem validation on startdate , enddate fileds. since these 2 properties compulsory fields in database, save button should disabled unless fill these 2 fields. startdate , enddate fileds not compulsory. attaching codes. can please spare few minutes @ code , provide suggestions? many thanks.
code behind
public partial class organisationtypesview : usercontrol { organisationtypeviewmodel _datacontext = new organisationtypeviewmodel(); public organisationtypesview() { initializecomponent(); this.datacontext = _datacontext; _datacontext.accountstartdate = datetime.now; _datacontext.accountenddate = datetime.now.addmonths(1); this.loaded += new routedeventhandler(organisationtypesview_loaded); } void organisationtypesview_loaded(object sender, routedeventargs e) { } } xmal
<wpftoolkit:datepicker grid.column="1" grid.row="4" name="dpaccstart" verticalalignment="top" selecteddate="{binding accountstartdate, mode=twoway, updatesourcetrigger=propertychanged, validatesondataerrors=true, notifyonvalidationerror=true}" /> <wpftoolkit:datepicker grid.column="1" grid.row="5" name="dpaccend" verticalalignment="top" selecteddate="{binding accountenddate, mode=twoway, updatesourcetrigger=propertychanged, validatesondataerrors=true, notifyonvalidationerror=true}"/> viewmodel
private datetime? _accountstartdate; private datetime? _accountenddate; public event propertychangedeventhandler propertychanged; [required(errormessage = "account start date required field.")] public datetime? accountstartdate { { return _accountstartdate; } set { if (_accountstartdate != datetime.minvalue && accountenddate != datetime.minvalue) { if (value > _accountenddate) { messagebox.show("start date must less end date"); value = this.accountstartdate; } } _accountstartdate = value; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs("accountstartdate")); } } } [required(errormessage = "account end date required field.")] public datetime? accountenddate { { return _accountenddate; } set { if (_accountstartdate != datetime.minvalue && accountenddate != datetime.minvalue) { if (_accountstartdate > value) { messagebox.show("end date must after start date"); value = this.accountenddate; } } _accountenddate = value; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs("accountenddate")); } } }
what want validation rule based on completness of bound class. let viewmodel implement inotifydataerrorinfo interface , implement geterrors, haserrors, etc.
finally add
validatesonnotifydataerrors=true to binding.
this allows check consistency of entire model, not single properties.
Comments
Post a Comment