c# - WPF: Validation vs. Converters -


with converter, can differentiate between @ least 4 types of behavior regarding update of source value:

  • converting proper value (-> update source)
  • returning null (-> indicate error)
  • throwing exception , activating exception validation rule (-> indicate error)
  • returning binding.donothing (-> don't update source, don't indicate error eiter)

with validationrule, can discriminate between success (-> update source) , failure (-> don't update source), cannot simulate behavior associated binding.donothing

is there way use validationrule in way similar binding.donothing behavior of converters?

the intents of converters , validationrules pretty different. converters take 1 value , make another. 4 cases mention common enough converting: it; it's null; blow up; ignore. validationrules yes/no things - they're valid or they're not. while might make sense have "ignore" option, there isn't one.

the closest semantically set isvalid = true in constructor, it's not precisely want.

public override validationresult validate(object value, cultureinfo cultureinfo)  {     try     {         // try normal setup/validation     }      catch      {         // handle exceptions, return false     }     // decide if want return false     // return true (equivalent nothing/ignore)     return new validationresult(true, null); } 

last thought have if need special cases, , try-catch or other logic blow up. thing can think of type check in validationrule, pretty dubious since you're creating undesirable dependency, bypass problems. i.e.

if (value specialtype) {     return new validationresult(true, null); } 

hth!

updated

or how ignorablevalidationrule?

public class ignorablevalidationrule : validationrule {     public bool ignore { get; set; } = false;      public override validationresult validate(object value, cultureinfo cultureinfo)     {         if (ignore) return new validationresult(true, null);          return new validationresult(false, "why ignore me?");     } }  <textbox.text>     <binding path="data">         <binding.validationrules>             <local:ignorablevalidationrule ignore="true"/> <!-- na na -->         </binding.validationrules>     </binding> </textbox.text> 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -