ValueConversion属性的好处是什么?

pun*_*r76 7 c# wpf msdn binding ivalueconverter

我没有实现的问题ValueConverter

在MSDN上,我找到了以下ValueConversion属性:

[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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这会让我感兴趣:

  • 什么时候可以使用?
  • 我什么时候应该使用它?

感谢您的提示!