locked
How to limit the selectable dates in datepicker control? RRS feed

  • Question

  • I want to limit some dates in datepicker control, for example: I only want to set 2011-10-22 and 2011-10-16 are selectable in Oct.

    Now I have a list of selectable dates in a year, I want to set other dates are unselectable. How can I implement it?

    Tuesday, October 18, 2011 9:17 AM

Answers

  • Hi,

    You could use the a the BlackoutDates property of the DatePicker class in combination with CalendarDateRange to represente a collection of dates that are not available for selection.

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
    
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
       datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(new DateTime(2011, 10, 01),new DateTime(2011, 10, 15)));
       datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(new DateTime(2011, 10, 17),new DateTime(2011, 10, 21)));
       datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(new DateTime(2011, 10, 23),new DateTime(2011, 10, 31)));
    }

    in the previous case the only selectable dates in month of October would be the 16th and the 22nd... 

    if you'd want to blackout all dates prior to the actual date, you can simply use:

    datePickerCtrl.BlackoutDates.AddDatesInPast();

     

    and for example if you'd want to blackout all weekend  days (Saturdays and Sundays) in the month
    you could use a similar approach...

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        List<DateTime> datelist = getWeekenddays();
        foreach (DateTime date in datelist)
        {
           datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(date));
        }
    }
    
    private List<DateTime> getWeekenddays()
    {
        List<DateTime> list = new List<DateTime>();
        int days =  DateTime.DaysInMonth(datePickerCtrl.DisplayDate.Year, datePickerCtrl.DisplayDate.Month);
        for (int i = 0; i < days; i++)
        {
           DateTime datetime = new DateTime(datePickerCtrl.DisplayDate.Year, cal.DisplayDate.Month, i+1);
           if (datetime.DayOfWeek == DayOfWeek.Saturday || datetime.DayOfWeek == DayOfWeek.Sunday)
           {
               list.Add(datetime);
           }
        }
        return list;
    } 

    Hope this helps

    If this answers your question, please Mark as an answer. Thank you!  

    Tuesday, October 18, 2011 4:59 PM

All replies

  • Hi,

    You could use the a the BlackoutDates property of the DatePicker class in combination with CalendarDateRange to represente a collection of dates that are not available for selection.

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
    
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
       datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(new DateTime(2011, 10, 01),new DateTime(2011, 10, 15)));
       datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(new DateTime(2011, 10, 17),new DateTime(2011, 10, 21)));
       datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(new DateTime(2011, 10, 23),new DateTime(2011, 10, 31)));
    }

    in the previous case the only selectable dates in month of October would be the 16th and the 22nd... 

    if you'd want to blackout all dates prior to the actual date, you can simply use:

    datePickerCtrl.BlackoutDates.AddDatesInPast();

     

    and for example if you'd want to blackout all weekend  days (Saturdays and Sundays) in the month
    you could use a similar approach...

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        List<DateTime> datelist = getWeekenddays();
        foreach (DateTime date in datelist)
        {
           datePickerCtrl.BlackoutDates.Add(new CalendarDateRange(date));
        }
    }
    
    private List<DateTime> getWeekenddays()
    {
        List<DateTime> list = new List<DateTime>();
        int days =  DateTime.DaysInMonth(datePickerCtrl.DisplayDate.Year, datePickerCtrl.DisplayDate.Month);
        for (int i = 0; i < days; i++)
        {
           DateTime datetime = new DateTime(datePickerCtrl.DisplayDate.Year, cal.DisplayDate.Month, i+1);
           if (datetime.DayOfWeek == DayOfWeek.Saturday || datetime.DayOfWeek == DayOfWeek.Sunday)
           {
               list.Add(datetime);
           }
        }
        return list;
    } 

    Hope this helps

    If this answers your question, please Mark as an answer. Thank you!  

    Tuesday, October 18, 2011 4:59 PM
  • Thank you very much, your solution solved my problem!

    Thank you again!

    Thursday, November 10, 2011 2:07 AM
  • Glad to ear it...:)

    You're very welcome!

    Thursday, November 10, 2011 9:29 AM