WPF Datepicker only lists selected dates

I'm not sure if this is possible, but is it possible to make only a list of dates selected in the wpf print date?

I need to make sure that use can only select a certain number of dates. I can do this with a drop down list, but with a dumper it would be much nicer.

Any ideas?

+4
source share
3 answers

DatePicker has the following properties to control which dates should be selected:

DisplayDateStart : The first date to be included in the Calendar popup.
DisplayDateEnd : The last date to be included in the Calendar popup.

So, if you have a list containing valid dates, you can set DisplayDateStart to the first item in the list and DisplayDateEnd to the last item in the list.

This will prevent users from choosing dates outside this range.

To handle cases where the allowed date list contains spaces, you can use the BlackoutDates property to make date ranges not selected.

So, to add dates that are not in the list as darkened, you can do something like the following.

The calendar pop-up window displays only the dates in the list, and dates that are not in the list are darkened, so they cannot be selected.

 var dates = new List<DateTime> { new DateTime(2013, 1, 5), new DateTime(2013, 1, 6), new DateTime(2013, 1, 7), new DateTime(2013, 1, 8), new DateTime(2013, 1, 15), new DateTime(2013, 1, 16), new DateTime(2013, 1, 28), new DateTime(2013, 1, 29), new DateTime(2013, 2, 9), new DateTime(2013, 2, 12), new DateTime(2013, 2, 13), new DateTime(2013, 2, 17), new DateTime(2013, 2, 18) }; var firstDate = dates.First(); var lastDate = dates.Last(); var dateCounter = firstDate; foreach (var d in dates.Skip(1)) { if (d.AddDays(-1).Date != dateCounter.Date) { dtp.BlackoutDates.Add( new CalendarDateRange(dateCounter.AddDays(1), d.AddDays(-1))); } dateCounter = d; } dtp.DisplayDateStart = firstDate; dtp.DisplayDateEnd = lastDate; 

If the valid dates are very far from each other, this probably will not be very user-friendly, as it would be rather unpleasant if 90% of the dates went out. In this case, a ComboBox might be better.

+7
source

You can do what you want with the BlackoutDates collection so that the user cannot select all the dates you want to avoid.

code:

 myDatePicker.BlackoutDates.Add(myCalendarDateRange); 

XAML:

 <DatePicker Name="myDatePicker"> <DatePicker.BlackoutDates> <CalendarDateRange Start="02.20.2013" End="02.22.2013" /> </DatePicker.BlackoutDates> </DatePicker> 
+3
source

You need to create your own control template. The easiest way is to take the default template (you can download it from the URL below) and change the necessary parts.

http://archive.msdn.microsoft.com/wpfsamples#themes

0
source

All Articles