DatePicker - Text not updating to date format

Jul 21, 2009 at 2:28 AM
Edited Jul 21, 2009 at 2:29 AM

When typing the date in the text box of the DatePicker control, if the date was, for example, "7/19/2009", and the user typed in "7-20-09", after the value was committed, the text would change to the format "7/20/2009". This is good, and what I expected. However, if the date value didn't change, the format was not updated. For example, if the date was "7/20/2009", and the user cleared that text and typed in "7-20-09", I would expect the text to be formatted as "7/20/2009". However, it instead stayed as the text "7-20-09".

I found that by changing the code in DatePicker.cs as follows, the behavior was consistent between changed and unchanged underlying dates:

Original Code

        private DateTime? SetTextBoxValue(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                SetValue(TextProperty, s);
                return this.SelectedDate;
            }
            else
            {
                DateTime? d = ParseText(s);

                if (d != null)
                {
                    SetValue(TextProperty, s);
                    return d;
                }
                else
                {
                    // If parse error:
                    // TextBox should have the latest valid selecteddate value:
                    if (this.SelectedDate != null)
                    {
                        string newtext = this.DateTimeToString((DateTime)this.SelectedDate);
                        SetValue(TextProperty, newtext);
                        return this.SelectedDate;
                    }
                    else
                    {
                        SetWaterMarkText();
                        return null;
                    }
                }
            }
        }

New Code

        private DateTime? SetTextBoxValue(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                SetValue(TextProperty, s);
                return this.SelectedDate;
            }
            else
            {
                DateTime? d = ParseText(s);

                if (d != null)
                {
                    SetValue(TextProperty, DateTimeToString(d.Value));
                    return d;
                }
                else
                {
                    // If parse error:
                    // TextBox should have the latest valid selecteddate value:
                    if (this.SelectedDate != null)
                    {
                        string newtext = this.DateTimeToString((DateTime)this.SelectedDate);
                        SetValue(TextProperty, newtext);
                        return this.SelectedDate;
                    }
                    else
                    {
                        SetWaterMarkText();
                        return null;
                    }
                }
            }
        }

The code changed was the second call to SetValue.

Are there any other side effects to changing the DatePicker code as shown so that the text formatting is applied even if the date doesn't change? If it is intended that the text formatting is not applied when the date doesn't change, could we at least get a flag that allows us to choose one way or the other?

Coordinator
Jul 22, 2009 at 1:14 AM

Hi Jaxoson -

Nice find :) The issue has been entered into our database. Thanks for the suggested fix too :)

-Eric, MSFT

Jul 24, 2009 at 9:56 PM

No problem. Thanks for looking into this. (And for the code so I could change it to do what I wanted it to do in the meantime.)