This is a migrated thread and some comments may be shown as answers.

DateTime text not updating after null value

1 Answer 37 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 06 Dec 2011, 10:15 PM
I have a problem that happens when we enter invalid date into the control and tab off.
When we tab off, it fired the propertyChanged event on our bound object. During the handling of that event we may sent the objects value back to a business valid value.

When we do this, and the date entered in a valid date, but outside the scope of allowed dates for our application, the date text in the control changes to the updated date value - as expected.
 
When the date entered is not valid, the bound object becomes null, and so we reset the bound object to a valid datetime, but in this case, the text of the control now shows the watermark, and not the value updated date.

We tried to force a binding.updatesource on the selectedDate property. We also tried to raise a property change on the lost focus event, and still only the watermark text shows. Upon looking into the control, the selectedDate is set to the correct value, it is that the CurrentDateTimeText and DateTimeText properties are empty, thus the watermark shows.

How can the controls SelectedDate contain the value, but the text properties do not?

In the end we found a workaround: during LostFocus event on the control, we set the controls DateTimeText property to the correct string to force it in. This works, but seems like there might be a better way to handle this.

1 Answer, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 12 Dec 2011, 03:51 PM
Hello Tom,

The best way to achieve this is to use the DisplayDateStart and DisplayDateEnd to prevent from wrong input with mouse. For the keyboard part you will have to use the ParseDateTimeValue event and check whether the parsing is successful. Here is sample code:
private void picker_ParseDateTimeValue(object sender, Telerik.Windows.Controls.ParseDateTimeEventArgs args)
{
    if (args.IsParsingSuccessful == false)
    {
        args.Result = new DateTime(2011, 1, 1); 
        args.IsParsingSuccessful = true;
    }
 
    if (args.Result.HasValue && args.Result.Value.Year > DateTime.Today.Year)
    {
        args.Result = new DateTime(2011, 12, 31);
        args.IsParsingSuccessful = true;
    }
}

I hope this will help you.

Kind regards,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
DateTimePicker
Asked by
Tom
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or