如何处理值转换器中的异常,以便可以显示自定义错误消息 [英] How to handle exception in Value converter so that custom error message can be displayed
问题描述
如果非号码输入到文本框中,我想要显示一个自定义错误消息(而不是默认的输入字符串格式错误)。
转换代码是:
public object ConvertBack(
对象值,
键入targetType,
对象参数
CultureInfo文化)
{
try
{
int minutes = System.Convert.ToInt32(value);
返回新的TimeSpan(0,minutes,0);
}
catch
{
throw new FormatException(请输入一个数字);
}
}
我在XAML中设置了ValidatesOnExceptions = True绑定。
但是,我遇到了以下MSDN文章,这解释了为什么上述不起作用:
数据绑定引擎不会捕获由用户提供的转换器抛出的异常,Convert方法抛出的任何异常或Convert方法调用的方法抛出的任何未捕获的异常被视为运行时间错误
我已经看到ValidatesOnExceptions在TypeConverters中捕获异常,所以我的具体问题是:
- 您将何时在ValueConverter上使用TypeConverter
- 假设TypeConverter不是上述问题的答案,那么我如何显示我的自定义错误消息在UI中
我将使用 ValidationRule
为此,转换器可以确保转换因为它只有在验证成功时被调用,并且可以使用附加的属性 Validation.Errors
将包含您的 ValidationRule
创建的错误输入不是你想要的。
eg (注意工具提示绑定)
< TextBox>
< TextBox.Style>
< Style TargetType ={x:Type TextBox}>
< Style.Triggers>
< Trigger Property =Validation.HasErrorValue =True>
< Setter Property =BackgroundValue =Pink/>
< Setter Property =ToolTipValue ={Binding RelativeSource = {RelativeSource Self},Path =(Validation.Errors)[0] .ErrorContent}/>
< / Trigger>
< /Style.Triggers>
< / Style>
< /TextBox.Style>
< TextBox.Text>
< Binding Path =Uri>
< Binding.ValidationRules>
< vr:UriValidationRule />
< /Binding.ValidationRules>
< Binding.Converter>
< vc:UriToStringConverter />
< /Binding.Converter>
< / Binding>
< /TextBox.Text>
< / TextBox>
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
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 property Validation.Errors
which will contain the errors your ValidationRule
creates if the input is not the way you want it.
e.g. (note the tooltip binding)
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Pink"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
<TextBox.Text>
<Binding Path="Uri">
<Binding.ValidationRules>
<vr:UriValidationRule />
</Binding.ValidationRules>
<Binding.Converter>
<vc:UriToStringConverter />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
这篇关于如何处理值转换器中的异常,以便可以显示自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!