c# - Add ValidationRule in XAML from code behind -


i trying add validationrule xaml code behind , and need have this:

<textbox.text>    <binding path="model.txt1.value" updatesourcetrigger="propertychanged" validatesondataerrors="true">       <binding.validationrules>          <localval:requiredvalidate />       </binding.validationrules>    </binding> </textbox.text> 

i have tried far:

frameworkelement selectedobject = fe_dragged_control; dependencyproperty property =                                controlbindingextensions.getdependencypropertyfromname("text", selectedobject.gettype()); binding binding = new binding("model." + selectedobject.name + ".value"); binding.updatesourcetrigger = updatesourcetrigger.propertychanged; binding.validatesondataerrors = true; requiredvalidate role = new requiredvalidate(); binding.validationrules.add(role); selectedobject.setbinding(property, binding); 

i found on google, getting following result (removed irrelevant properties readability:

<textbox text="{binding validatesondataerrors=true,                  path=model.txt0.value,                  updatesourcetrigger=propertychanged}" > 

how have the result need (the 1st code)? thanks

you should check viewmodel. sample worked following test case.

<textbox x:name="txt0"> 

validation

using system.globalization; using system.windows.controls;  namespace wpfapplication2 {     public class requiredvalidate : validationrule     {         public override validationresult validate(object value, cultureinfo cultureinfo)         {             return value != null ? validationresult.validresult : new validationresult(false, "value required");         }     } } 

code behind

    private void initializevalidation()     {          frameworkelement selectedobject = txt0;         dependencyproperty property =             getdependencypropertybyname(selectedobject, "textproperty");         binding binding = new binding("model.txt0");         binding.updatesourcetrigger = updatesourcetrigger.propertychanged;         binding.validatesondataerrors = true;         requiredvalidate role = new requiredvalidate();         binding.validationrules.add(role);         selectedobject.setbinding(property, binding);     }      public static dependencyproperty getdependencypropertybyname(dependencyobject dependencyobject, string dpname)     {         return getdependencypropertybyname(dependencyobject.gettype(), dpname);     }      public static dependencyproperty getdependencypropertybyname(type dependencyobjecttype, string dpname)     {         dependencyproperty dp = null;          var fieldinfo = dependencyobjecttype.getfield(dpname, bindingflags.public | bindingflags.static | bindingflags.flattenhierarchy);         if (fieldinfo != null)         {             dp = fieldinfo.getvalue(null) dependencyproperty;         }          return dp;     } 

and viewmodels

public class mainwindowviewmodel {     public mainwindowviewmodel()     {         model = new model();     }      public model model { get; set; } }  public class model {     public model()     {         txt0 = 42;         txt1 = 99;     }      public int? txt0 { get; set; }     public int? txt1 { get; set; } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -