WPFでIDataErrorInfoを使用する場合、パラメーターをバリデーターに渡す方法があります。たとえば、DueDateDatepickerがあります。新しいタスクを検証するときは、許可される日付を今日以降に制限したいのですが、編集するときは、期限が過ぎたタスクを編集できるため、今日より前のDueDatesを許可する必要があります。
XamlのMyDatePicker(.Net 4.0)
<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;
}
}