我正在实现一个函数“在StartDate和EndDate上的WPF验证(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)
{
}
}
XmaI位
<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}"/>
视图模型
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"));
}
}
}
答案 0 :(得分:0)
您想要的是基于绑定类的完整性的验证规则。 让ViewModel实现INotifyDataErrorInfo接口并实现GetErrors,HasErrors等。
最后添加
ValidatesOnNotifyDataErrors=True
到绑定。
这使您可以检查整个模型的一致性,而不仅仅是单个属性。
答案 1 :(得分:0)
我认为除了验证之外,你应该使用CanExecute处理程序的Save命令,它会检查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处理程序的条件自动检查启用或禁用日期的值
答案 2 :(得分:0)
我通过删除代码“_dataContext.AccountStartDate = DateTime.Now; _dataContext.AccountEndDate = DateTime.Now.AddMonths(1);”来解决问题。在代码背后。由于我给出了StartDate和EndDate字段的初始日期,它会自动获取初始日期,因此将激活保存按钮。