我在ViewModel
中进行了数据验证。当我加载View
,有效性验证不改变TextBox
的内容检查,通过加载错误样式设置为TextBox
如何停止验证触发器在wpf中自动启动
这里的观点意思是代码:
XAML
<TextBox {...} Text="{Binding Path=ProductName,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"/>
在ViewModel
上,验证通过数据注释进行:
Code
private string _productName;
[Required(AllowEmptyStrings = false, ErrorMessage = "The Product Name can't be null or empty.")]
[StringLength(50, ErrorMessage = "The Product Name can't be longer than 50.")]
[Uniqueness(Entities.Product, ErrorMessage = "A Product with that Name already exists ")]
public string ProductName
{
get { return _productName; }
set
{
_productName = value;
SaveProduct.OnCanExecuteChanged();
OnPropertyChanged("ProductName");
}
}
如何在视图加载时停止验证触发?
我不希望TextBox
在插入数据之前显示错误。
AttributeBased验证有这个问题,直到我知道什么都不能做Directly.IDataErrorInfo/INotifyDataErrorInfo是更好的选择,因为我们从setter中设置它们,并且这个问题不存在,再加上我们可以通过ourelve处理所有事情。 – ethicallogics