这个问题似乎与之前的一个问题非常相似:previous,但是案例有点不同(也许更好地展示了问题),虽然目标相同。
xaml:
当这段代码独立运行时,对于输入的
问题:当在 Visual Studio 下运行程序时,输入
xaml:
<TextBox Text="{local:ExceptionBinding Path=Color1}" />
cs:
public class ExceptionBinding : Binding
{
public ExceptionBinding()
{
ValidationRules.Add(new ExceptionValidationRule());
}
}
vm:
Color _color1;
public string Color1
{
get { return (new ColorConverter()).ConvertToString(_color1); }
set { _color1 = (Color)ColorConverter.ConvertFromString(value); }
}
当这段代码独立运行时,对于输入的
123
,会在 TextBox
周围显示红色边框(字段值不会更改)。输入 red
或 #FF0000
将删除边框并更新字段值。问题:当在 Visual Studio 下运行程序时,输入
123
会在 ConvertFromString
上抛出异常(顺带提一下,这是未记录的):
如何防止 Visual Studio 中断程序执行?An exception of type 'System.FormatException' occurred in PresentationCore.dll but was not handled in user code
Additional information: Token is not valid.