【发布时间】:2016-08-11 12:14:20
【问题描述】:
我的 WPF 表单包含单个属性“PersonName”。我希望在 NULL OR EMPTY 上提出错误。我从 Data Annotations 得到了解决方案,我参考了教程http://www.c-sharpcorner.com/uploadfile/20c06b/screen-validation-with-data-annotations-in-wpf/
我的 XAML 是
<TextBox Text="{Binding PersonName, UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
<Button Content="Save" IsDefault="True" Command="{Binding SaveCommand}"
IsEnabled="{Binding }" Width="150" Height="40"/>
我只能在 onPropertyChange 事件中看到错误。如果我直接点击按钮而不触摸TextBox 表示,我无法看到错误。我怎样才能在 Button 点击时触发相同的功能。
我的要求是 PersonName 不应该是 NULL 或 EMPTY,如果属性是 NULL 或 EMPTY,那么我需要禁用按钮,基于 IDataErrorInfo 而不是 Property.Length
最初,我们不应用任何验证。一旦错误的数据输入或没有点击按钮的值,那么我需要一个验证。
【问题讨论】:
标签: c# wpf xaml data-annotations idataerrorinfo