DatePicker Validation Issues

Oct 5, 2009 at 11:02 PM

In my application is a view that has a Date field, as well as a Date field within a DataGrid control. Both implementations use a template control that uses a TextBlock and DatePicker pairing for display and editing respectively.

My application is MVVM structured, and validation occurs primarily as attributes on the ViewModel properties. The validation is working properly with one catch: when someone changes an invalid date, using the date picker to select a proper date, the validation rule is malfunctioning (more likely, im misunderstanding its difference from normal binding IDEI validations). Below I'll provide snippets of the three pieces, and explain why I can't seem to debug it properly.

Toolkit column implementation:

                    <toolkit:DataGridTemplateColumn x:Name="IncurredDateColumn" Header="Incurred Date" MinWidth="100" >
                                <toolkit:DatePicker SelectedDate="{Binding Path=IncurredDate, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" SelectedDateFormat="Short">
                                <TextBlock Text="{Binding Path=IncurredDate, StringFormat=d}" />

ViewModel Property - Validation Attributes, INPC implemented:

public DateTime IncurredDate
get { return _model.IncurredDate; }
if (value == _model.IncurredDate)
_model.IncurredDate = value;

Validation Attribute Implementation:

    public sealed class ValidationAttributeDateTime : ValidationAttribute
 public override string Validate(string propertyName, object propertyValue)
// error message var validationError = propertyName + " must be a valid Date.";

// if null, return error if (propertyValue == null) return validationError;

// cast value as DateTime var date = ((DateTime)(propertyValue));

// Invalid dates found on import are set to 01/01/0001. // We catch them here for correction. var invalidValue = new DateTime(1, 1, 1);

// Return validation error if Date equals the invalid value, or is in the future. return date == invalidValue || date > DateTime.Now ? validationError : null;





The validation is occuring whenever a view makes use of the view model... meaning, when it binds and first fills the control with its value. However, when I edit the date fields in my view, Validate() is being sparked on entering the date picker control, like as soon as i click into it to type or select a date. Isn't this incorrect? If set to PropertyChanged should it not wait until I have either (1) selected a new value using DatePicker's calendar object, or (2) typed a new date myself and tabbed away much like a text box?

Am I misusing this control? Is this a bug? How come validation is not occuring AFTER i have selected a date and tabbed away from the control?


Thanks for the help ahead of time,