Search code examples
wpfvalidationivalueconverter

How to set error message through converter in wpf


NOTE: this question is 8 years old! the WPF strategy for handing custom errors has significantly changed since it was asked. please see https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-binding-validation

I have a custom numeric text box bound to a double. on error, the default error message that shows (as a tooltip) is "can not convert string to double" or something similar.

How can I change that custom message?

I'm trying to do it through the converter:

class MyConverter : IValueConverter
  {

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return 1234.23;
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
           // if( value is not good)
           // ?? throw exception ?? 
      }
}

Solution

  • You should be able to throw your own errors within the converter, and if you have the ValidateOnDataErrors=true then your validation template will display the exceptions error message. I believe,I haven't done this in a while!