I'm using WPF with MVVM implementation, and have a WPF DatePicker control. I've set the default date on my property using DateTime.Now, but I would like to disable all the Dates on the DatePicker control that fall before the current date so that the User cannot select any earlier dates.
I've tried setting this in xaml using FallbackValue={x:Static sys:DateTime.Now} in my SelectedDate property, and {x:Static sys:DateTime.Now} in most of the other properties (from another source) but the dates before the current dates are still displ开发者_如何学Cayed and still selectable.
Is there an easy way to accomplish this?
Maybe you're looking BlackoutDates property?
Here is more info of using the DatePicker control (including BlackoutDates):
WPF Toolkit: Calendar & DatePicker Walkthrough
For example if you want to set your blackout dates from 1/1/2000 to present day you can do:
<Calendar.BlackoutDates>
<CalendarDateRange Start="1/1/2000" End="{x:Static System:DateTime.Today}" />
</Calendar.BlackoutDates>
Where System namespace is defined as
xmlns:System="clr-namespace:System;assembly=mscorlib"
You can use DisplayDateStart and DisplayDateEnd properties to select what dates should be displayed in the control or if you want something more complex you can use BlackOutDates to select exactly what dates you don't want to be selectable.
CalendarDateRange cdr = new CalendarDateRange(DateTime.MinValue, DateTime.Today); myCalendar.BlackoutDates.Add(cdr);
You can use
MyDatePicker.BlackoutDates.AddDatesInPast();
See https://msdn.microsoft.com/en-us/library/system.windows.controls.calendarblackoutdatescollection.adddatesinpast(v=vs.110).aspx
精彩评论