I am achieving a function "WPF Validation On StartDate and EndDate (StartDate less than Enddate)", I write code in code behind to throw an exception if the EndDate is less than the StartDate, and now it works. But I met a problem about the validation on StartDate and EndDate fileds. Since these two properties are compulsory fields in my database, the Save button should be disabled unless you fill these two fields. But now StartDate and EndDate fileds are not compulsory. I am attaching my codes. Can you please spare a few minutes to look at my code and provide some suggestions? Many thanks.
我正在实现一个函数“在StartDate和EndDate上的WPF验证(StartDate小于EndDate)”,如果EndDate小于StartDate,我将在代码后面编写代码以抛出异常,现在它可以工作了。但是我遇到了一个关于StartDate和EndDate文件的验证的问题。由于这两个属性是数据库中的必填项字段,因此应该禁用Save按钮,除非填充这两个字段。但是现在开始和开始文件并不是强制性的。我附上我的密码。你能抽出几分钟时间看看我的代码并提供一些建议吗?多谢。
Code Behind
后面的代码
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
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"));
}
}
}
3 个解决方案
#1
0
What you want is a validation rule based on completness of your bound class. Let your ViewModel implement the INotifyDataErrorInfo interface and implement GetErrors, HasErrors, etc.
您需要的是基于绑定类的复杂性的验证规则。让您的ViewModel实现INotifyDataErrorInfo接口并实现GetErrors、HasErrors等。
Finally add
最后添加
ValidatesOnNotifyDataErrors=True
to the binding.
绑定。
This allows you to check for consistency of the entire model, not just single properties.
这允许您检查整个模型的一致性,而不仅仅是单个属性。
#2
0
I suppose besides of validation you should use Save command both with CanExecute handler, which would check the values of dpAccStart and AccountEndDate, something like this:
我想除了验证之外,您应该使用CanExecute handler的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;
}
Then in XAML you can assign your SaveCmd command save button:
然后在XAML中你可以分配你的SaveCmd命令保存按钮:
<Button Command="{Binding SaveCmd}">
After this the WPF will automaticly check the values of of dates making it enabled or disabled according to conditions of you CanExecute handler
在此之后,WPF将自动检查根据您的CanExecute处理程序的条件使其启用或禁用的日期的值。
#3
0
I fixed the problem by removing the codes "_dataContext.AccountStartDate = DateTime.Now; _dataContext.AccountEndDate = DateTime.Now.AddMonths(1);" in code behind. Since I give an initial date for the StartDate and EndDate field, it gets the initial date automatically, so the save button will be activated.
我通过删除代码“_dataContext”解决了这个问题。AccountStartDate = DateTime.Now;_dataContext。AccountEndDate = DateTime.Now.AddMonths(1);由于我为StartDate和EndDate字段提供了初始日期,它将自动获得初始日期,因此save按钮将被激活。
#1
0
What you want is a validation rule based on completness of your bound class. Let your ViewModel implement the INotifyDataErrorInfo interface and implement GetErrors, HasErrors, etc.
您需要的是基于绑定类的复杂性的验证规则。让您的ViewModel实现INotifyDataErrorInfo接口并实现GetErrors、HasErrors等。
Finally add
最后添加
ValidatesOnNotifyDataErrors=True
to the binding.
绑定。
This allows you to check for consistency of the entire model, not just single properties.
这允许您检查整个模型的一致性,而不仅仅是单个属性。
#2
0
I suppose besides of validation you should use Save command both with CanExecute handler, which would check the values of dpAccStart and AccountEndDate, something like this:
我想除了验证之外,您应该使用CanExecute handler的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;
}
Then in XAML you can assign your SaveCmd command save button:
然后在XAML中你可以分配你的SaveCmd命令保存按钮:
<Button Command="{Binding SaveCmd}">
After this the WPF will automaticly check the values of of dates making it enabled or disabled according to conditions of you CanExecute handler
在此之后,WPF将自动检查根据您的CanExecute处理程序的条件使其启用或禁用的日期的值。
#3
0
I fixed the problem by removing the codes "_dataContext.AccountStartDate = DateTime.Now; _dataContext.AccountEndDate = DateTime.Now.AddMonths(1);" in code behind. Since I give an initial date for the StartDate and EndDate field, it gets the initial date automatically, so the save button will be activated.
我通过删除代码“_dataContext”解决了这个问题。AccountStartDate = DateTime.Now;_dataContext。AccountEndDate = DateTime.Now.AddMonths(1);由于我为StartDate和EndDate字段提供了初始日期,它将自动获得初始日期,因此save按钮将被激活。