IValueConverter的最佳做法是什么?

时间:2021-02-13 22:26:44
  • What is best practise for IValueConverter?
  • IValueConverter的最佳做法是什么?
  • Is it ok to put Exception in Convert method or should it return "something"?
  • 可以将Exception放在Convert方法中,还是应该返回“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());
    }
}

3 个解决方案

#1


13  

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

如果转换失败(格式错误的值,类型......),请返回DependencyProperty.UnsetValue。

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

它表示转换器没有产生任何值,并且绑定使用FallbackValue(如果可用)或默认值。

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

此外,您应该将具有特定于文化的转换或不变转换的数据转换为安全的一面。

#2


5  

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}

#3


2  

You've ignored CultureInfo while parsing the string.

您在解析字符串时忽略了CultureInfo。

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

总是考虑传递的文化信息,否则它将始终在Thread的CurrentCulture上工作。

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

我可以给出一些像“7.34,123”这样的东西作为输入,你的代码会起作用吗?

#1


13  

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

如果转换失败(格式错误的值,类型......),请返回DependencyProperty.UnsetValue。

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

它表示转换器没有产生任何值,并且绑定使用FallbackValue(如果可用)或默认值。

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

此外,您应该将具有特定于文化的转换或不变转换的数据转换为安全的一面。

#2


5  

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}

#3


2  

You've ignored CultureInfo while parsing the string.

您在解析字符串时忽略了CultureInfo。

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

总是考虑传递的文化信息,否则它将始终在Thread的CurrentCulture上工作。

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

我可以给出一些像“7.34,123”这样的东西作为输入,你的代码会起作用吗?