7

What is best practise for IValueConverter: Is it ok to put Exception in Convert method or should it return "something"?

Here is an 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 is float) //Edited to support CultureInfo.CurrentCulture,
            return string.Format(culture, "{0:n}{1}", ((float)value) * 100, "%");

        //** Is it ok to put Exception here or should I return "something" here? **
        throw new Exception("Can't convert from " + value.GetType().Name + ". Expected type if float.");
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("Converting back is not implemented in " + this.GetType());
    }
}
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Amir Rezaei
  • 4,948
  • 6
  • 33
  • 49
  • Does this answer your question? [Best practice when not implementing IValueConvert.ConvertBack](https://stackoverflow.com/questions/265515/best-practice-when-not-implementing-ivalueconvert-convertback) – Herohtar Jan 06 '20 at 04:29
  • Does this answer your question? [WPF ValueConverter - Standard return for unconvertible value](https://stackoverflow.com/questions/5992769/wpf-valueconverter-standard-return-for-unconvertible-value) – StayOnTarget Jul 05 '22 at 16:55

3 Answers3

14

If you fail to convert (malformed values, types, ...), return DependencyProperty.UnsetValue.

It indicates that the converter produced no value and that the binding uses the FallbackValue, if available, or the default value instead.

Also, you should convert data with culture-specific conversion or invariant conversions to be on the safe side.

Jaroslav Jandek
  • 9,463
  • 1
  • 28
  • 30
  • Should I return string.Empty? – Amir Rezaei Feb 09 '11 at 12:29
  • 1
    `return DependencyProperty.UnsetValue;` There are other return values that you can modify the behaviour of the binding engine with. You can follow the link I have provided in my answer to read about it further. Here is a sample of using `DependencyProperty.UnsetValue`: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx – Jaroslav Jandek Feb 09 '11 at 12:32
  • Per [this link](https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx), you can use `Binding.DoNothing` if the binding shouldn't set a particular value. This should be more use in things like multivalueconverters though. – Lauraducky Dec 06 '17 at 04:11
8

I personally recommend using singleton converters. Then you don't have to create an instance at every usage site, but can reference the converter like this:

Converter={x:Static SomeNamespace:SomeConverter.Instance}
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
3

You've ignored CultureInfo while parsing the string.

Always take in to account the culture info passed otherwise it would always work on Thread's CurrentCulture.

I could give some thing like "7.34,123" as input, would your code work?

Vijay Sirigiri
  • 4,653
  • 29
  • 31