ValidatesOnExptions如何工作

MaR*_*Ruf 5 validation wpf

基于这个例子

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validatesonexceptions.aspx

似乎ValidatesOnExceptions负责捕获自定义异常并将其添加到Validation.Errors集合中.

问题是即使将ValidatesOnExceptions设置为false,我也有相同的行为

有人可以解释我错过了什么吗?

谢谢

nem*_*esv 5

ValidatesOnExceptions用于自定义异常显示。但是,如果您已TextBox绑定到某个int属性,那么在绑定发生之前会发生转换,这可能会导致“红色边框”。

尝试一下

<TextBox Text="{Binding IntField}"/>
<TextBox Text="{Binding StringField, ValidatesOnExceptions=False}"/>
<TextBox Text="{Binding StringField, ValidatesOnExceptions=True}"/>

public int IntField { get; set; }

private string stringField;
public string StringField
{
    get { return stringField; }
    set
    {
        throw new Exception();  text = stringField; }
    }
}
Run Code Online (Sandbox Code Playgroud)

在每个文本框中输入一个数字:

  1. 由于数据转换而出现红色边框
  2. 没有红色边框,因为ValidatesOnExceptions false
  3. 红色边框,因为 ValidatesOnExceptions true

我希望它有帮助。