【发布时间】:2010-01-24 01:42:52
【问题描述】:
在 WPF 中使用 IDataErrorInfo 时,是否可以将参数传递给验证器。例如,我有一个 DueDate 日期选择器。在验证新任务时,我想将允许的日期限制为今天或以后,但在编辑时我需要允许在今天之前的 DueDates,因为可以编辑过期的任务。
我在 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;
}
}
【问题讨论】:
标签: c# wpf validation xaml idataerrorinfo