- Каков наилучший метод для IValueConverter ?
- Можно ли помещать исключение в метод Convert или оно должно возвращать "что-то"?
Вот пример:
[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());
}
}