0

我有一个 Silverlight 表单,它执行基于异常的数据验证。我学习了如何通过以下方式进行数据验证:

设置要验证的控件如下:

<TextBox Text="{Binding Mode=TwoWay,NotifyOnValidationError=True, Source={StaticResource docSan}, Path= metadati.paziente.residenza, ValidatesOnExceptions=True}"/>

使目标属性按如下方式工作

public new string residenza
    {
        get { return base.residenza; }
        set
        {
            if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
            base.residenza = value;
        }
    }

基类以某种INotifyPropertyChanged方式定义非验证属性

不幸的是,VS2010 在设计时警告我每个文本框的异常。这不会阻止应用程序运行(它工作正常),但它只是烦人。

有人知道如何告诉 VS,如果在设计时没有指定任何值,那么代码自然会抛出?

4

1 回答 1

1

如果我理解正确,是if ... throw设置器中的语句导致设计器发出警告吗?

我认为您可以使用DesignerProperties.IsInDesignTool来防止此行在设计时运行:

set
{
    if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
    {
        if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
    }
    base.residenza = value;
}
于 2012-07-01T18:41:37.863 回答