Reputation: 1070
I have a DatePicker so the user can select dates.
I want that the user to select only the available Date.
and the availabe dates are stored in a List
So far I have this:
<DatePicker x:Name="DatePicker"
SelectedDate="{Binding SearchEngineCompassLogView.DateSearch,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DataContext="{StaticResource CompassLogView}">
</DatePicker>
Upvotes: 4
Views: 2222
Reputation: 19296
You can use BlackoutDates
property but in this solution you must specified DisplayDateStart
and DisplayDateEnd
.
The BlackoutDates
is a collection of dates that are not available for selection (msdn).
Example:
<DatePicker x:Name="datePicker"
Loaded="datePicker_Loaded"
DisplayDateStart="2000/01/01"
DisplayDateEnd="2050/01/01"
/>
Loaded event handler:
private void datePicker_Loaded(object sender, RoutedEventArgs e)
{
DatePicker picker = sender as DatePicker;
if (picker.DisplayDateStart == null || picker.DisplayDateEnd == null) return;
picker.BlackoutDates.Clear();
DateTime start = picker.DisplayDateStart.Value;
DateTime end = picker.DisplayDateEnd.Value;
while (start <= end)
{
if (!availableDates.Contains(start))
{
picker.BlackoutDates.Add(new CalendarDateRange(start, start));
}
start = start.AddDays(1);
}
}
Collection with available dates:
List<DateTime> availableDates = new List<DateTime>
{
new DateTime(2013, 03, 01),
new DateTime(2013, 03, 02),
new DateTime(2013, 03, 03),
new DateTime(2013, 03, 31),
new DateTime(2013, 02, 01),
new DateTime(2013, 02, 02),
new DateTime(2013, 05, 01),
new DateTime(2013, 05, 02)
};
Upvotes: 1