如何处理值转换器中的异常以便可以显示自定义错误消息
我有一个文本框,它绑定到一个具有 Timespan 类型属性的类,并编写了一个值转换器来将字符串转换为 TimeSpan。
如果在文本框中输入非数字,我希望显示自定义错误消息(而不是默认的“输入字符串格式错误”)。
转换器代码是:
public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
try
{
int minutes = System.Convert.ToInt32(value);
return new TimeSpan(0, minutes, 0);
}
catch
{
throw new FormatException("Please enter a number");
}
}
我已在 XAML 绑定中设置“ValidatesOnExceptions=True”。
但是,我遇到了以下 MSDN 文章,它解释了为什么上述内容不起作用:
“数据绑定引擎不会捕获用户提供的转换器引发的异常。Convert 方法引发的任何异常,或Convert 方法调用的方法引发的任何未捕获的异常都被视为运行时错误”
我读过“ValidatesOnExceptions 确实捕获了 TypeConverters 中的异常,所以我的具体问题是:
- 何时使用 TypeConverter 而不是 ValueConverter
- 假设TypeConverter 不能解决上述问题,如何在 UI 中显示自定义错误消息
I have a textbox that is bound to a class with a property of type Timespan, and have written a value converter to convert a string into TimeSpan.
If a non number is entered into the textbox, I would like a custom error message to be displayed (rather than the default 'input string is in the wrong format').
The converter code is:
public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
try
{
int minutes = System.Convert.ToInt32(value);
return new TimeSpan(0, minutes, 0);
}
catch
{
throw new FormatException("Please enter a number");
}
}
I have set 'ValidatesOnExceptions=True' in the XAML binding.
However, I have come across the following MSDN article, which explains why the above will not work:
"The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors"
I have read that 'ValidatesOnExceptions does catch exceptions in TypeConverters, so my specific questions are:
- When would you use a TypeConverter over a ValueConverter
- Assuming a TypeConverter isn't the answer to the issue above, how can I display my custom error message in the UI
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用
ValidationRule
< /a> 为此,转换器可以确保转换有效,因为只有在验证成功时才会调用它,并且您可以使用附加属性Validation.Errors
其中将包含您的ValidationRule
如果输入不是您想要的方式,则创建。例如(注意工具提示绑定)
I would use a
ValidationRule
for that, this way the converter can be sure that the conversion works since it only is called if validation succeeds and you can make use of the attached propertyValidation.Errors
which will contain the errors yourValidationRule
creates if the input is not the way you want it.e.g. (note the tooltip binding)
我使用验证和转换器来接受
null
和数字XAML:
Code Behind:
ValidationRule :
Converter :
I used validation and converter to accept
null
and numbersXAML:
Code Behind:
ValidationRule :
Converter :
您不应该从转换器中抛出异常。我将实现 IDataErrorInfo 并在其上实现错误和字符串。请检查 https://web.archive.org/web/20110528131712/http://www.codegod.biz/WebAppCodeGod/WPF-IDataErrorInfo-and-Databinding-AID416.aspx。
丹尼尔·HTH
You shouldn't throw exceptions from the converter. I would implement IDataErrorInfo and implement the Error and String on that. Please check https://web.archive.org/web/20110528131712/http://www.codegod.biz/WebAppCodeGod/WPF-IDataErrorInfo-and-Databinding-AID416.aspx.
HTH daniell