c# - Launch ExceptionValidationRule from specific context -


i have class client :

public class client : inotifypropertychanged {      private string m_strcode;     public string strcode     {         { return m_strcode; }         set         {              if (codeisvalide(strcode))             {                                   m_strcode = value;                 firepropertychangedevent("strcode");              }             else             {                 throw new applicationexception("bad data !");             }              firepropertychangedevent("strcode");          }     }      private string m_strname;     public string strname     {         { return m_strname; }         set         {              if (nameisvalide(strname))             {                 m_strname = value;                 firepropertychangedevent("strname");              }             else             {                 throw new applicationexception("bad data! ");             }              firepropertychangedevent("strname");          }     }      public client(string code, string name)     {         strcode = code;         strname = name;     }      public client()     {      }      public event propertychangedeventhandler propertychanged;      private void firepropertychangedevent(string propertyname)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }  } 

on wpf window, have 2 textbox : on client.strcode, , other client.strnom.

here xaml :

<textbox x:name="textbox_code"          horizontalalignment="left"          height="25"          margin="106,230,0,0"          textwrapping="wrap"          verticalalignment="top"          width="78">   <textbox.text>     <binding path="strcode"              updatesourcetrigger="propertychanged">       <binding.validationrules>         <exceptionvalidationrule />       </binding.validationrules>     </binding>   </textbox.text> </textbox>  <textbox x:name="textbox_name"          horizontalalignment="left"          height="25"          margin="106,230,0,0"          textwrapping="wrap"          verticalalignment="top"          width="78">   <textbox.text>     <binding path="strname"              updatesourcetrigger="propertychanged">       <binding.validationrules>         <exceptionvalidationrule />       </binding.validationrules>     </binding>   </textbox.text> </textbox> 

the data input verifiyng validationprocess. if validation false, appear warning label near textbox.

my problem : if load bad data ( database) collection of client object. load use set() of strcode , strname, and, if data bad, exceptionvalidationrule cannot throwing ( think it's because exceptionvalidationrule not invoked textbox binded). ( i'm not english, it's not esay explain sorry).

i think need specify when want invoke verifiy process.

anyone have advices please ?

if want, create sample vs project in order share problem.

edit : if use custom validator class, ok !

just question, in order force validation test, need this, when select line in datagrid :

    private void mydatagrid_selectedcellschanged(object sender, selectedcellschangedeventargs e)     {         // affiche le code évt sélectionné dans le tableau, dans les champs modifiable ( en haut de l'écran )          var item = mydatagrid.selecteditem client;         if ((item != null))         {             textbox_code.text = item.strcode;             textbox_name.text = item.strname;         }     } 

:

textbox_code.text = item.strcode; textbox_name.text = item.strname; 

if remove 2 line, textbox corectly initialized because of binding, validation process not invoked. why ? there way force the validation process , use full binding whitout :

textbox_code.text = item.strcode; textbox_name.text = item.strname; 

thanks :)

thanks lot :)

best regards,

nixeus :)

well if understand requiring correctly,

you not want exceptions raised if property set apart xaml view.

my preferred solution not use exceptionvalidationrule @ , prefer custom validationrule. solve issue. example shows how create , use such feature.

example custom validation rule:

public class coderangerule : validationrule {   public override validationresult validate(object value, cultureinfo cultureinfo) {     int strcode = -1;     if (value != null && !int32.tryparse(value.tostring(), out strcode))       return new validationresult(false, "illegal strcode");     if (strcode < 9999 && strcode > 0) // modify suit validity conditions       return new validationresult(true, null);     return new validationresult(false, "strcode not within acceptable code range");   } } 

and xaml:

<textbox x:name="textbox_code" horizontalalignment="left" height="25" margin="106,230,0,0" textwrapping="wrap" verticalalignment="top" width="78">     <textbox.text>         <binding path="strcode"  updatesourcetrigger="propertychanged">             <binding.validationrules>                 <local:coderangerule />             </binding.validationrules>         </binding>     </textbox.text> </textbox> 

or

if not want use custom validationrule whatever reason, can keep exceptionvalidationrule in view , either add try catch before calling setter database load or in your

public class client : inotifypropertychanged 

add property(say enum values kdatabaseload, kview) indicate if object worked on view / database. in setter of strcode , strname check if enum kview before throwing exception.

now when database load , create client objects assign it's enum kdatabaseload before setting strname , strcode. make sure switch enum kview once done load.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -