Stojdza
Stojdza

Reputation: 445

WPF datepicker BlackoutDates on SelectedDateChanged

I have two datepickers: "dpInputDate" and "dpDueDate". now DueDate must not be smaller than InputDate. So I used BlackoutDates.AddDatesInPast(); and it works fine when application starts. Now thing is when I change the selected date of dpInputDate, than dpDueDate automatically gets the same value. Problem is that the number of dates that are blacked out remains the same. Here is the code that I used:

 private void dpInputDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
    {
        dpDueDate.SelectedDate = dpInputDate.SelectedDate;

        dpDueDate.BlackoutDates.Clear();
        dpDueDate.BlackoutDates.AddDatesInPast();
    }

How to manage this?

UPDATE:

private void dpDueDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
    {            
        if (dpDueDate.SelectedDate != null)
        {
            DateTime date = (DateTime)dpDueDate.SelectedDate;
            dpDueDate.BlackoutDates.Clear();
            dpDueDate.BlackoutDates.Add(new CalendarDateRange(new DateTime(1 / 1 / 2001), date));
        }
        else 
        {
            dpDueDate.BlackoutDates.AddDatesInPast();
        }
    }

Upvotes: 1

Views: 2186

Answers (1)

Mashton
Mashton

Reputation: 6415

AddDatesInPast() blocks out all dates from DateTime.Now (i.e. today), not from the SelectedDate backwards.

So you will need to calculate your own CalendarDateRange, and set the BlackoutDates to your new range. There is an overload that takes two dates, which you can use for your purposes http://msdn.microsoft.com/en-us/library/cc672700(v=vs.110).aspx

Upvotes: 2

Related Questions