wpf - A generic Boolean Convertor -


i have following converter

  [valueconversion(typeof(bool), typeof(visibility))] public sealed class booltovisibilityconverter : ivalueconverter {     public visibility truevalue { get; set; }     public visibility falsevalue { get; set; }      public booltovisibilityconverter()     {         // set defaults         truevalue = visibility.visible;         falsevalue = visibility.collapsed;     }      public object convert(object value, type targettype,         object parameter, cultureinfo culture)     {         if (!(value bool))             return null;         return (bool)value ? truevalue : falsevalue;     }      public object convertback(object value, type targettype,         object parameter, cultureinfo culture)     {         if (equals(value, truevalue))             return true;         if (equals(value, falsevalue))             return false;         return null;     } } 
<conv:boolconverter x:key="enablestyleconvertor" truevalue="visible" falsevalue="collapsed" /> 

is there way make more generic, ie can return type of object?

you make truevalue , falsevalue of type object. may want update equals code see if objects implement icomparable though.

[valueconversion(typeof(bool), typeof(object))] public sealed class myconverter : ivalueconverter {     public object truevalue { get; set; }     public object falsevalue { get; set; }      public object convert(object value, type targettype,         object parameter, cultureinfo culture)     {         if (!(value bool))             return null;         return (bool)value ? truevalue : falsevalue;     }      public object convertback(object value, type targettype,         object parameter, cultureinfo culture)     {         if (isequal(value, truevalue))             return true;         if (isequal(value, falsevalue))             return false;         return null;     }      private static bool isequal(object x, object y) {         if (equals(x, y))           return true;          icomparable c = x icomparable;         if (c != null)            return (c.compareto(y) == 0);          return false;     } } 

to use you'd need explicitly define values though:

<local:myconverter>     <local:myconverter.truevalue>         <visibility>visible</visibility>     </local:myconverter.truevalue>     <local:myconverter.falsevalue>         <visibility>collapsed</visibility>     </local:myconverter.falsevalue> </local:myconverter> 

edit:

a generic version this:

[valueconversion(typeof(bool), typeof(object))] public sealed class myconverter<t> : ivalueconverter {     public t truevalue { get; set; }     public t falsevalue { get; set; }      public object convert(object value, type targettype,         object parameter, cultureinfo culture) {         if (!(value bool))             return null;         return (bool)value ? truevalue : falsevalue;     }      public object convertback(object value, type targettype,         object parameter, cultureinfo culture) {         if (isequal(value, truevalue))             return true;         if (isequal(value, falsevalue))             return false;         return null;     }      private static bool isequal(object x, object y) {         if (equals(x, y))             return true;          icomparable c = x icomparable;         if (c != null)             return (c.compareto(y) == 0);          return false;     } } 

this isn't accessible xaml though. xaml 2009 has additional support generics, loose xaml files (i.e. not compiled).


Comments

Popular posts from this blog

how to build hyperlink for query string in php -

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

queue - mq_receive: message too long -