views:

228

answers:

1

When using IDataErrorInfo in WPF is there a way to pass parameters to the validator. For instance I have a DueDate Datepicker. When validating for a new task I want to restrict the date allowed to today or later but when editing I need to allow for DueDates before today since a task can be edited that is past due.

My DatePicker in Xaml (.Net 4.0)

<DatePicker SelectedDate="{Binding Path=SelectedIssue.IssDueDate,
            ValidatesOnDataErrors=True}" />

My 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;
        }
    }
+1  A: 

You could just use a custom validator on the binding. Or you could maintain a IsNew internal state on the the Issue object instance until it is no longer considered new.

dhopton
Why does there always have to be 7 ways to do anything? I did some research on this and it looks like this will work. My Model is Linq to SQL datasets and I have added partial classes with validation rules that inplement IDataErrorInfo. If I use a custom validator for dates should I leave the IDataErrorInfo validation in as well or should I just add checks to the custom date validator to ensure Min and Max date are not outside the allowed range. The later seems simplest but it feels like bad form to have database validation in two places.
Mike B