开发者

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-validat开发者_Python百科ion

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 ?? 
      }
}


Have a look at the validation Mechanisms in wpf.Its exactly what you are looking for


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!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜