8

I have no problems with the implementation of a ValueConverter.

On MSDN I found the ValueConversion attribute:

[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = (DateTime)value;
        return date.ToShortDateString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value as string;
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return DependencyProperty.UnsetValue;
    }
}

and it would interest me:

  • When can I use it?
  • When should I use it?

Thanks for any hints!

punker76
  • 14,326
  • 5
  • 58
  • 96
  • 5
    From MSDN: `When implementing the IValueConverter interface, it is a good practice to decorate the implementation with a ValueConversionAttribute attribute to indicate to development tools the data types involved in the conversion.` – nemesv Jul 29 '13 at 14:10
  • @nemesv your're right, but I don't found this on so, thx My eyes must have been closed. – punker76 Jul 29 '13 at 14:12
  • 2
    Also: `The ValueConversionAttribute attribute enables designer tools, such as Windows Presentation Foundation (WPF) Designer for Visual Studio, to discover value converters to assist developers in setting up bindings. ` – Matthew Watson Jul 29 '13 at 14:14

0 Answers0