Mike B
Mike B

Reputation: 2672

How to set property in IDataErrorInfo validator from Xaml

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;
        }
    }

Upvotes: 2

Views: 1874

Answers (1)

Dominic Hopton
Dominic Hopton

Reputation: 7292

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.

Upvotes: 2

Related Questions