c# - WPF Data Validation inside of your DataContext? -


i'm using custom validationrule validate data entered textbox.

<textbox>     <binding path="name" updatesourcetrigger="propertychanged">         <binding.validationrules>             <util:alphanumericvalidationrule/>             <util:stringlengthifenteredrule min="1"/>         </binding.validationrules>     </binding> </textbox> 

which works fine verify alphanumeric string entered, , it's length greater or equal 1.

the issue form textbox part of has "submit" button bound function inside of datacontext. when hit submit want able able know whether or not fields valid, inside data context.

i've been looking little bit, , best way i've see check if there errors in form in code-behind file, , pass information along datacontext. solution less ideal, i'm wondering if there's sort of bindable property or i've missed provide functionality?

tl;dr how tell datacontext whether or not field validated using xaml?

the best way i've found implement idataerrorinfo. example below.

xaml

<textbox text="{binding name, validatesondataerrors=true, updatesourcetrigger=propertychanged}"/> 

datacontext

using system.componentmodel;  class viewmodel, idataerrorinfo {     public string name {get;set;}      private bool formcontainserrors;     private string formerror;     private alphanumericvalidationrule alphanumericvalidation;      public viewmodel()     {         this.alphanumericvalidation = new alphanumericvalidationrule();         this.formcontainserrors = false;     }      public string error     {         { return this.formerror; }     }      public string this[string columnname]     {                 {             this.formerror = null;             switch (columnname)             {                 case "name":                     this.formerror = (string)this.alphanumericvalidation.validate(this.name, null).errorcontent;                     break;             }             this.formcontainserrors = (this.formerror != null);             return this.formerror;         }     } } 

with can check "formcontainserrors" know whether or not contains errors


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -