Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best practise for IValueConverter?

Tags:

c#

.net

wpf

xaml

  • 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());
    }
}
like image 984
Amir Rezaei Avatar asked Feb 09 '11 11:02

Amir Rezaei


People also ask

What are methods present in IValueConverter interface?

As advertised, it just implements the two required methods, called Convert() and ConvertBack(). The Convert() methods assumes that it receives a string as the input (the value parameter) and then converts it to a Boolean true or false value, with a fallback value of false.

What is IValueConverter?

The IValueConverter interface consists of two methods, Convert() and ConvertBack() . Convert method gets called when source updates target object. ConvertBack method gets called when target updates source object.

How to use value converter in WPF?

To use the converter you need to implement the interface of the converter class in the XAML page of WPF and need to declare the resource. After declaring the resource there is the need to use it with binding. See the following XAML code example. XAML Example: The code example uses a converter in WPF XAML.

Which interface in code allows us to create value Converters?

If you want to associate a value converter with a binding, create a class that implements the IValueConverter interface and then implement the Convert and ConvertBack methods.


3 Answers

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.

like image 179
Jaroslav Jandek Avatar answered Sep 23 '22 05:09

Jaroslav Jandek


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}
like image 44
Matěj Zábský Avatar answered Sep 19 '22 05:09

Matěj Zábský


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?

like image 29
Vijay Sirigiri Avatar answered Sep 19 '22 05:09

Vijay Sirigiri