c# - Using WPF validation -


i have dialog in project user enters values in , when hits ok add item database. using entity framework, adding database code this:

transactionitem _item = new transactionitem();                 _item.doctorid = (int)cmbdoctor.selectedvalue;                 _item.transactioncategoryid = (int)_dlg.cmbcat.selectedvalue;                 _item.transactionmethodid = (int)_dlg.cmbmethod.selectedvalue;                 _item.amount = int.parse(_dlg.txtamount.text);                 _item.documentid = _dlg.txtdocnum.text;                 _item.info = _dlg.txtinfo.text;                 _item.date = _dlg.dtedate.selecteddate.tostring();                 _db.transactionitems.add(_item);                 _db.savechanges(); 

but problem there nothing bind , enable validating. have tried making empty object in window , bind text box it, had own problems , didn't work expected. want when users enter values or when hits ok, check if of fields valid (for example 1 of problems if user didn't enter value, still valid though stringnotnull validator enabled, important problem automatically set textbox's text null , mark null value).

and have made own validator , here example of how implemented them on 1 of textboxes:

<textbox name="txtamount" horizontalalignment="left" height="23" margin="83,169,0,0" verticalalignment="top" width="224" tag="t">     <textbox.text>         <binding  path="myitem" elementname="mywindow" updatesourcetrigger="propertychanged">             <binding.validationrules>                 <validators:stringnullvalidationrule/>                 <validators:isnumericvalidationrule/>             </binding.validationrules>         </binding>     </textbox.text> </textbox> 

why don't create property in viewmodel each value user needs enter, , bind it? use these properties when adding item. example:

viewmodel:

public int amount { get; set; }  ...  public void additem() {     transactionitem _item = new transactionitem();      // ...      _item.amount = amount; } 

xaml:

<textbox name="txtamount" horizontalalignment="left" height="23" margin="83,169,0,0" verticalalignment="top" width="224" tag="t">     <textbox.text>         <binding path="datacontext.amount" elementname="mywindow">             <binding.validationrules>                 <validators:stringnullvalidationrule/>                 <validators:isnumericvalidationrule/>             </binding.validationrules>         </binding>     </textbox.text> </textbox> 

i recommend having @ inotifydataerrorinfo interface (or idataerrorinfo interface if you're using .net 4.0 or lower) implement validations.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -