IDataErrorInfo不起作用

时间:2013-11-27 19:01:03

标签: c# xaml textbox idataerrorinfo

我希望文本框的边框为红色,而另一个文本框则告诉错误。

C#

 class BandsVM : ObservableObject, IPage, IDataErrorInfo
    {

        [Required(ErrorMessage = "De naam is verplicht")]
        [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Er zijn geen speciale tekens toegelaten")]
        [StringLength(50, MinimumLength = 1, ErrorMessage = "De naam moet tussen de 1 en 50 karakters bevatten ")]
        private string insertName;
        public string InsertName
        {
            get { return insertName;  }
            set { insertName = value; }
        }

XAML:                                                                                                                                                                                                                                                                                                 

<TextBox Validation.ErrorTemplate="{StaticResource ErrorTemplate}" Text="{Binding InsertName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextboxError}"/>

1 个答案:

答案 0 :(得分:0)

因为您继承了ObservableObject,我假设它实现了INotifyPropertyChanged

属性InsertName应调用PropertyChanged事件处理程序,以通知wpf绑定引擎评估属性更改。然后,这将调用您的this []索引,IDataErrorInfo用它来评估验证错误。

将您的财产更改为:

public string InsertName
{
    get { return insertName;  }
    set { 
        if (value != insertname)
        {
            insertName = value; 
        }

        OnPropertyChanged(() => InsertName);
        // or OnPropertyChanged("InsertName");
    }
}