DateTime text not updating after null value

2 posts, 0 answers
  1. Tom
    Tom avatar
    1 posts
    Member since:
    Jun 2009

    Posted 06 Dec 2011 Link to this post

    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.
  2. Ivo
    Ivo avatar
    390 posts

    Posted 12 Dec 2011 Link to this post

    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,
    the Telerik team

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

Back to Top