【发布时间】:2016-01-05 10:50:09
【问题描述】:
验证不起作用的可能原因是什么?
public class DatabaseObj : ValidatableModel, IFormattable
{
[Required(ErrorMessage="Hostname is required")]
public string Hostname
{
get { return _hostname; }
set { SetProperty(ref _hostname, value); }
}
}
[Serializable]
public abstract class ValidatableModel : Model, INotifyDataErrorInfo
{
}
[Serializable]
public abstract class Model : INotifyPropertyChanged
{
}
在 xaml 中,
<TextBox Text="{Binding DatabaseObj.Hostname, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=true, NotifyOnValidationError=true}"
Grid.Column="1" Grid.Row="1" Margin="0,1,91,10" HorizontalAlignment="Stretch"/>
但是,当我编译和运行时,我清空了文本框,没有显示错误消息,颜色仍然保持不变。
*对象类更新
【问题讨论】:
-
看看this question,或许对你有帮助
-
你实现了 IDataErrorInfo 吗?
-
我找到了解决方案。我没有使用 SetProperty,而是使用 SetPropertyAndValidate