none
Silverlight 4 - DatePicker control [Validation error for invalid date]

    Question

  • Hi,

    I am currently working on Silverlight 4 with MVVM pattern.

    i have one datepicker control directly binding to the one of the properties of the model class. From UI when ever i enter invalid date in the datepicker like '22/22/22' or 'abcdefg' then red border is not comming.

    I have already set all the properties of the DatePicker like NotifyOnError etc.

    I don't no whether it's possible in silverlight 4?

    Can anyone please shed some light on this?

    Thanks,

     Mahesh.

     

    Tuesday, April 26, 2011 11:29 PM

Answers

  • The way I did this was add another string property in the Model class which worked as a wrapper for my datetime property. 

    private DateTime dt;
            public DateTime MyDateField
            {
                get { return dt; }
                set { dt = value; }
            }
    
            //This field will be bound to the UI instead of the above MyDateField property
            public string MyDateTime
            {
                get { return MyDateField.ToString(); }
                set
                {
                    bool result;
                    result = DateTime.TryParse(value, out dt);
                    if(!result)
                        throw new ValidationException("Please enter a valid Date.");
                }
            }

    This should validate the date you enter in your UI. Please note that you will  have to bind to the MyDateTime string property instead of the older DateTime property.

    Hope this works for you.

    Wednesday, April 27, 2011 2:34 PM

All replies

  • The way I did this was add another string property in the Model class which worked as a wrapper for my datetime property. 

    private DateTime dt;
            public DateTime MyDateField
            {
                get { return dt; }
                set { dt = value; }
            }
    
            //This field will be bound to the UI instead of the above MyDateField property
            public string MyDateTime
            {
                get { return MyDateField.ToString(); }
                set
                {
                    bool result;
                    result = DateTime.TryParse(value, out dt);
                    if(!result)
                        throw new ValidationException("Please enter a valid Date.");
                }
            }

    This should validate the date you enter in your UI. Please note that you will  have to bind to the MyDateTime string property instead of the older DateTime property.

    Hope this works for you.

    Wednesday, April 27, 2011 2:34 PM
  • This didn't work for me. Maybe the DatePicker 4 version got an update that made this not work anymore.
    Friday, May 9, 2014 4:07 PM