2
在WPF中使用IDataErrorInfo時,有一種將參數傳遞給驗證器的方法。例如,我有一個DueDate Datepicker。在驗證新任務時,我希望限制今天或以後允許的日期,但是在編輯時,由於可以編輯已過期的任務,因此需要在今天之前允許DueDates。如何從Xaml中設置IDataErrorInfo驗證器中的屬性
我在XAML的DatePicker(.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;
}
}
爲什麼總是有7種方法可以做任何事情?我在這方面做了一些研究,看起來這會起作用。我的模型是Linq to SQL數據集,我已經添加了帶有填充IDataErrorInfo的驗證規則的部分類。如果我爲日期使用自定義驗證程序,則應該保留IDataErrorInfo驗證,或者只應將檢查程序添加到自定義日期驗證程序以確保Min和Max日期不在允許的範圍之外。後者似乎最簡單,但在兩個地方進行數據庫驗證感覺像是一種糟糕的形式。 – 2010-01-25 15:17:21