Reputation: 161
i'm developing WPF application in that i'm trying to disable the past days in calender. any way is there to disable the past days.please let me know.
thanks
Upvotes: 10
Views: 16513
Reputation: 96
I like a @sohailby answer but I just want to change little by using blackoutdates
<DatePicker.BlackoutDates>
<CalendarDateRange End="{x:Static sys:DateTime.Today}" Start="{x:Static sys:DateTime.MinValue}" />
</DatePicker.BlackoutDates>
Upvotes: 1
Reputation: 171
In WPF and you want to use MVVM binding to your view model. This approach is more general and lets you do boundary checking within your unit tests.
<DatePicker SelectedDate="{Binding SelectedDate}" DisplayDateStart="{Binding MinAvailDate}" DisplayDateEnd="{Binding MaxAvailDate}"></DatePicker>
In the view model....
private DateTime _minAvailDate= DateTime.UtcNow;
public DateTime MinAvailDate { get { return _minAvailDate; }
set { _minAvailDate = value; OnPropertyChanged("MinAvailDate"); }
Set MinAvailDate = DateTime.Now, or adjust the default for your time zone.
Implement MaxAvailDate in a similar way, as well as you binding to the SelectedDate.
Upvotes: 0
Reputation: 1198
You have to set the DisplayDateStart
attribute with Today's date
<DatePicker Name="dt_StartDateFrom" DisplayDateStart="{x:Static sys:DateTime.Today}">
</DatePicker>
Make sure you have set the
xmlns:sys="clr-namespace:System;assembly=mscorlib"
in your <UserControl>
tag to be able to use the sys:
parameter
P.S. To Disable future dates, you can use DisplayDateEnd
attribute
Upvotes: 18
Reputation: 606
This is a neater solution to the problem in case we want to add it through xaml.
Upvotes: 1
Reputation: 208
To block out the past dates (everything before today), you can simply use the Framework's 4.0+ method : AddDatesInPast()
myCalendar.BlackoutDates.AddDatesInPast();
It does the same as the accepted answer but is more straightforward.
Upvotes: 2
Reputation: 172
You can disable the past days in calender in this way : -
Caldr.DisplayDateStart = DateTime.Today;
Upvotes: 3
Reputation: 161
I got the solution by trying myself.
In Window_load
just add this line for calendar
.
Caldate.BlackoutDates.Add(new CalendarDateRange(new DateTime(1990, 1, 1),
DateTime.Now.AddDays(-1)));
It will block previous dates.
Upvotes: 4
Reputation: 69372
You can set the DisplayDateStart property to today.
myCalendar.DisplayDateStart = DateTime.Today;
Instead of hiding all previous values, if you want to black them out, you can do this.
CalendarDateRange cdr = new CalendarDateRange(DateTime.MinValue, DateTime.Today);
myCalendar.BlackoutDates.Add(cdr);
Upvotes: 13