开发者

IDataErrorInfo with ValueConverter

I'm somehow doing it wrong, but I can't figure it out:

I have a model like this:

public class Person : IDataErrorInfo {
  public DateTime Birthdate {
    get { return _birthdate; }
    set {
      if (!Valid(value))
        AddError("Birthdate", "Birthdate not valid");

      _birthdate = value;
    }
  }
}

A ValueConverter like this:

public class DateToStringConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    return date.Date.ToShortDateString();
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    DateTime result;

    if (DateT开发者_JAVA技巧ime.TryParse(text, out result))
    {
      return result;
    }

    return DependencyProperty.UnsetValue;
  }
}

And a View like this:

<TextBox Text="{Binding Person.Birthdate,
                        Mode=TwoWay,
                        Converter={StaticResource DateToStringConverter},
                        ValidatesOnDataErrors=True}" />

If someone modifies an valid date like "1.1.1950" into an invalid date like "1.1.abc", the value does not get through to the Person and doesn't invalidate it. But a red border around the birthdate-textbox is shown. How can I keep the invalid text (to be modified by the user) and register an error for IDataErrorInfo?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜