我正在实现一个函数"WPF Validation On StartDate and EndDate(StartDate小于EndDate)",我在代码后面写代码,以便在EndDate小于StartDate时抛出异常,现在它可以工作了。但我遇到了一个关于StartDate和EndDate文件验证的问题。由于这两个属性是我的数据库中的必填字段,除非您填写这两个字段,否则"保存"按钮应该被禁用。但现在StartDate和EndDate文件不是强制性的。我正在附上我的代码。你能抽出几分钟时间看看我的代码并提供一些建议吗?非常感谢。
代码隐藏
public partial class OrganisationTypeSView : UserControl
{
OrganisationTypeViewModel _dataContext = new OrganisationTypeViewModel();
public OrganisationTypeSView()
{
InitializeComponent();
this.DataContext = _dataContext;
_dataContext.AccountStartDate = DateTime.Now;
_dataContext.AccountEndDate = DateTime.Now.AddMonths(1);
this.Loaded += new RoutedEventHandler(OrganisationTypeSView_Loaded);
}
void OrganisationTypeSView_Loaded(object sender, RoutedEventArgs e)
{
}
}
xmal
<WPFToolkit:DatePicker Grid.Column="1" Grid.Row="4" Name="dpAccStart" VerticalAlignment="Top"
SelectedDate="{Binding AccountStartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" />
<WPFToolkit:DatePicker Grid.Column="1" Grid.Row="5" Name="dpAccEnd" VerticalAlignment="Top"
SelectedDate="{Binding AccountEndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
ViewModel
private DateTime? _AccountStartDate;
private DateTime? _AccountEndDate;
public event PropertyChangedEventHandler PropertyChanged;
[Required(ErrorMessage = "Account Start Date is a required field.")]
public DateTime? AccountStartDate
{
get { return _AccountStartDate; }
set
{
if (_AccountStartDate != DateTime.MinValue && AccountEndDate != DateTime.MinValue)
{
if (value > _AccountEndDate)
{
MessageBox.Show("Start date must be less than End date");
value = this.AccountStartDate;
}
}
_AccountStartDate = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("AccountStartDate"));
}
}
}
[Required(ErrorMessage = "Account End Date is a required field.")]
public DateTime? AccountEndDate
{
get { return _AccountEndDate; }
set
{
if (_AccountStartDate != DateTime.MinValue && AccountEndDate != DateTime.MinValue)
{
if (_AccountStartDate > value)
{
MessageBox.Show("End date must be after Start date");
value = this.AccountEndDate;
}
}
_AccountEndDate = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("AccountEndDate"));
}
}
}
您想要的是一个基于绑定类的完整性的验证规则。让您的ViewModel实现INotifyDataErrorInfo接口并实现GetErrors、HasErrors等
最后添加
ValidatesOnNotifyDataErrors=True
绑定。
这允许您检查整个模型的一致性,而不仅仅是单个属性。
我想除了验证之外,您还应该将Save命令与CanExecute处理程序一起使用,后者将检查dpAccStart和AccountEndDate的值,类似于以下内容:
private DateTime? _AccountStartDate;
private DateTime? _AccountEndDate;
//Your code
RelayCommand _saveCommand;
public ICommand SaveCmd
{
get
{
if (_saveCommand == null)
_saveCommand = new RelayCommand(ExecuteSaveCommand, CanExecuteCommand);
return _saveCommand;
}
}
private void ExecuteSaveCommand(object parameter)
{
//your saving logic
}
private bool CanExecuteCommand(object parameter)
{
if (string.IsNullOrEmpty(_AccountStartDate) ||
string.IsNullOrEmpty(_AccountEndDate))
return false;
return true;
}
然后在XAML中,您可以指定SaveCmd命令保存按钮:
<Button Command="{Binding SaveCmd}">
在此之后,WPF将根据CanExecute处理程序
我通过删除代码后面的代码"_dataContext.AccountStartDate=DateTime.Now;_dataContext_AccountEndDate=DateTime.Now.AddMonths(1);"解决了这个问题。由于我为StartDate和EndDate字段提供了初始日期,它会自动获得初始日期,因此保存按钮将被激活。