Parse error with only year ShortDatePattern

2 posts, 0 answers
  1. Maciej
    Maciej avatar
    7 posts
    Member since:
    Oct 2014

    Posted 21 Jan 2015 Link to this post

    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?
  2. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 23 Jan 2015 Link to this post

    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.

     
  3. DevCraft banner
Back to Top