在 WPF 中使用 IDataErrorInfo 时,有一种方法可以将参数传递给验证器。例如我有一个 DueDate 日期选择器。在验证新任务时,我想将允许的日期限制在今天或以后,但在编辑时,我需要允许截止日期在今天之前,因为可以编辑逾期的任务。
Xaml (.Net 4.0) 中的我的 DatePicker
<DatePicker SelectedDate="{Binding Path=SelectedIssue.IssDueDate,
ValidatesOnDataErrors=True}" />
我的 IErrorDataInfo
namespace OITaskManager.Model
{
public partial class Issue : IDataErrorInfo
{
// I want to set these values from the Xaml
public DateTime minDate = new DateTime(2009, 1, 1);
public DateTime maxDate = new DateTime(2025, 12, 31);
public string this[string columnName]
{
get
{
if (columnName == "IssDueDate")
{
if (IssDueDate < minDate || IssDueDate > maxDate)
{
return "Due Date must be later than " + minDate.Date +
" and earlier than " + maxDate.Date;
}
return null;
}
return null;
}
}
我是一名优秀的程序员,十分优秀!