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

Parse error with only year ShortDatePattern

1 Answer 51 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Maciej
Top achievements
Rank 1
Maciej asked on 21 Jan 2015, 01:04 PM
Hi Telerik Team

I try to make my own YearPicker with additional int properties. Everything works fine except one case. When I set ShortDatePattern to 'yyyy' it displays error while typing year. When I set this pattern to 'MMMM yyyy' or even '/yyyy' it's ok and it work. But with 'yyyy' i've got parse error and SelectedDate is null. Dates from calendar work fine, only with typing there is a problem. Here is code of my class

public class YearPicker : RadDatePicker
    {
        public static readonly DependencyProperty SelectedYearProperty =
            DependencyProperty.Register("SelectedYear", typeof(int), typeof(YearPicker), new PropertyMetadata(1, PropertyChangedCallback));
         
        public YearPicker()
        {
            this.Culture = new CultureInfo(Thread.CurrentThread.CurrentUICulture.Name)
                {
                    DateTimeFormat =
                    {
                        ShortDatePattern = "/yyyy"
                    }
                };
 
            this.DateSelectionMode = DateSelectionMode.Year;
 
            if (!SelectedDate.HasValue)
            {
                this.SelectedDate = new DateTime(this.SelectedYear, 1, 1);
            }
            else
            {
                this.SelectedYear = this.SelectedDate.Value.Year;
            }
 
            this.SelectionChanged += this.OnSelectionChanged;
        }
 
        public int SelectedYear
        {
            get
            {
                return (int)this.GetValue(SelectedYearProperty);
            }
 
            set
            {
                this.SetValue(SelectedYearProperty, value);
            }
        }
 
        private new DateSelectionMode DateSelectionMode
        {
            set
            {
                base.DateSelectionMode = value;
            }
        }
//rest of functions with SeletecYear logic
    }
 

Can anyone tell me what is the reason of this behavior and maybe solution on some workaround?

1 Answer, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 23 Jan 2015, 09:22 AM
Hi Maciej,

What I can suggest you would be to set the ParseExact property of the control True, this way the year will be parsed correctly. However this will still display error while typing, so you can also implement a custom parsing in order avoid that. Please check the following article from our help documentation:
http://docs.telerik.com/devtools/wpf/controls/raddatetimepicker/how-to/implement-custom-parsing

Hope this helps.

Regards,
Kalin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
DatePicker
Asked by
Maciej
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Share this question
or