wpf - Trouble Parameterizing ValidationRules with Dependency Properties -


so have written following dp , validationrule:

public class comparisonvalue : dependencyobject {     public object comparisonobject     {         { return (object)getvalue(comparisonobjectprop); }         set {              setvalue(comparisonobjectprop, value);          }     }     public static readonly dependencyproperty comparisonobjectprop =         dependencyproperty.register("comparisonobject", typeof(object), typeof(comparisonvalue), new uipropertymetadata(null)); }  public class objectcomparisonvalidator : validationrule {     private comparisonvalue _objecttocompare;     public comparisonvalue objecttocompare     {                 {             return _objecttocompare;         }         set         {             _objecttocompare = value;         }      }      public override validationresult validate(object value, cultureinfo cultureinfo)     {         if (value != null)         {             if (!value.equals(objecttocompare.comparisonobject))             {                 return new validationresult(false, "values not equal");             }             else             {                 return new validationresult(true, null);             }         }         else         {             if (value != objecttocompare.comparisonobject)             {                 return new validationresult(false, "values not equal");             }             else             {                 return new validationresult(true, null);             }         }     } } 

then in xaml have following markup:

<usercontrol.resources>     <l:enumtostringconverter x:key="customenumconverter"/>     <l:booleantobrushconverter x:key="booleantobrushconverter"/>     <l:objectcomparisonvalidator x:key="objectcomparisonvalidator"/>     <l:comparisonvalue x:key="comparisonvalue"/> </usercontrol.resources>  ....              <textbox grid.row="2" grid.column="1" grid.columnspan="2" height="25" text="{binding path=networkkey.value, updatesourcetrigger=propertychanged}">                 <textbox.background>                     <binding path="networkkey.changed" converter="{staticresource booleantobrushconverter}">                         <binding.converterparameter>                             <x:array type="brush">                                 <solidcolorbrush color="yellow"/>                                 <solidcolorbrush color="white"/>                             </x:array>                         </binding.converterparameter>                     </binding>                 </textbox.background>             </textbox>  ....              <textbox grid.row="3" grid.column="1" grid.columnspan="2" height="25">                                     <textbox.text>                     <binding path="duplicatenetworkkey.value" updatesourcetrigger="propertychanged">                         <binding.validationrules>                             <l:objectcomparisonvalidator>                                 <l:objectcomparisonvalidator.objecttocompare>                                     <l:comparisonvalue comparisonobject="{binding path=networkkey.value}"/>                                 </l:objectcomparisonvalidator.objecttocompare>                             </l:objectcomparisonvalidator>                         </binding.validationrules>                     </binding>                 </textbox.text>                 <textbox.background>                     <binding path="duplicatenetworkkey.changed" converter="{staticresource booleantobrushconverter}">                         <binding.converterparameter>                             <x:array type="brush">                                 <solidcolorbrush color="yellow"/>                                 <solidcolorbrush color="white"/>                             </x:array>                         </binding.converterparameter>                     </binding>                 </textbox.background>             </textbox> 

now problem having validate method fo validation rule gets invoked, when binding networkkey gets triggered, setter in comparisonvalue object never gets invoked, time validation rule runs, comparisonobject property of objectcomparisonvalidator.objecttocompare null, , validation fails. whats wrong binding have comparisonobject?

just bit of clarification, type of networkkey , duplicatekey (props in vm) inpc classes. heres code them well:

public class valuefield<t> : achangereportingviewmodel, inotifypropertychanged {     private t _originalval;     public t originalval     {                 {             return _originalval;         }         set         {             _originalval = value;             value = value;             changed = false;             propertychanged(this, new propertychangedeventargs("originalval"));         }     }     private t _value;     public t value     {                 {             return _value;         }         set         {             _value = value;             if (_value == null)             {                 if (_originalval != null) changed = true;             }             else             {                 changed = !_value.equals(_originalval);             }             propertychanged(this, new propertychangedeventargs("value"));         }     }     private boolean _changed;     public boolean changed     {                 {             return _changed;         }         set         {             if (_changed != value)             {                 if (value) changemade();                 else changereversed();             }             _changed = value;             propertychanged(this, new propertychangedeventargs("changed"));         }     }      public event propertychangedeventhandler propertychanged = delegate { }; } 

<binding path="duplicatenetworkkey.value" updatesourcetrigger="propertychanged">                     <binding.validationrules>                         <l:objectcomparisonvalidator>                             <l:objectcomparisonvalidator.objecttocompare>                                 <l:comparisonvalue comparisonobject="{binding path=networkkey.value}"/>                             </l:objectcomparisonvalidator.objecttocompare>                         </l:objectcomparisonvalidator>                     </binding.validationrules>                 </binding> 

the inner binding object not part of visual tree, , not inherit data context of parent. bind outside of visual tree, use x:reference binding:

<l:comparisonvalue comparisonobject="{binding source={x:reference root}                                        path=datacontext.networkkey.value}"/> 

it's similar elementname binding, can't outside of visual tree. note "root" in example name of root ui element.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -