ivalueconverter - Issue in chaining IValueConvertes in WPF becauase of target type -


i trying chain converters town's answer in is there way chain multiple value converters in xaml??

i make individual converters more strict having targettype check :-

if (targettype != typeof(bool))         throw new invalidoperationexception("the target must      boolean"); 

but chain fails end target type different target @ each stage.

i can remove type check make less strict given in of examples on so, prefer chaining respects each converter's type check well. e.g. better unit testing etc.

also interface ivalueconverter doesn't expose target type, find difficult add check myself.

 public class inversebooleanconverter : ivalueconverter     {     public object convert(object value, type targettype, object parameter, cultureinfo culture)         {         if (targettype != typeof(bool))             throw new invalidoperationexception("the target must boolean");          if (!(value bool))             throw new argumentexception("argument 'value' must of type bool");          return !(bool)value;         }          ....      }  [valueconversion(typeof(bool), typeof(visibility))] public class visibilityfromboolconverter : ivalueconverter     {     public object convert(object value, type targettype, object parameter, cultureinfo culture)         {         if (targettype != typeof(visibility))             throw new invalidoperationexception("the target must visibility");          if (!(value bool))             throw new argumentexception("argument 'value' must of type bool");          var isvisible = (bool)value;         return isvisible ? visibility.visible : visibility.collapsed;         }         ....      } 

and composite :-

            <converters:valueconvertergroup x:key="invertandvisible">                   <converters:inversebooleanconverter />                   <converters:visibilityfromboolconverter />             </converters:valueconvertergroup> 

but exception "the target must boolean" inversebooleanconverter expects target bool instead of visibility (the end target of chain).

the original valueconvertergroup code passes final targettype each stage, why checks failing. need modify behaviour pass in next converters targettype instead:

[valueconversion(typeof(bool), typeof(visibility))] public class valueconvertergroup : list<ivalueconverter>, ivalueconverter {     #region ivalueconverter members      public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         (int = 0; < this.count(); i++)         {             var targ = (i == this.count() - 1)                  ? targettype                  : (this[i + 1].gettype().getcustomattributes(typeof(valueconversionattribute), false).first() valueconversionattribute).sourcetype;             value = this[i].convert(value, targ, parameter, culture);         }         if (value.gettype() != (this.gettype().getcustomattributes(typeof(valueconversionattribute), false).first() valueconversionattribute).targettype)             throw new invalidoperationexception("last target must of type " + targettype.name);         return value;     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     }      #endregion       } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -