c# - What is best practise for IValueConverter? -
- what best practise ivalueconverter?
- is ok put exception in convert method or should return "something"?
here example:
[valueconversion(typeof(float), typeof(string))] public class percentconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { if (value == null || string.isnullorempty(value.tostring())) return string.empty; if (value float) //edited support cultureinfo.currentculture, return string.format(culture, "{0:n}{1}", ((float)value) * 100, "%"); //** ok put exception here or should return "something" here? ** throw new exception("can't convert " + value.gettype().name + ". expected type if float."); } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notsupportedexception("converting not implemented in " + this.gettype()); } }
if fail convert (malformed values, types, ...), return dependencyproperty.unsetvalue.
it indicates converter produced no value , binding uses fallbackvalue, if available, or default value instead.
also, should convert data culture-specific conversion or invariant conversions on safe side.
Comments
Post a Comment