如何在 WPF 的日历中禁用过去的日子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17401488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:06:54  来源:igfitidea点击:

how to disable past days in calender in WPF?

wpf

提问by CnuSeran

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.

我正在开发 WPF 应用程序,因为我试图在日历中禁用过去几天。有什么办法可以禁用过去的日子。请告诉我。

thanks

谢谢

采纳答案by CnuSeran

I got the solution by trying myself.

我通过自己尝试得到了解决方案。

In Window_loadjust add this line for calendar.

Window_load刚加入这一行calendar

Caldate.BlackoutDates.Add(new CalendarDateRange(new DateTime(1990, 1, 1), 
DateTime.Now.AddDays(-1)));

It will block previous dates.

它将阻止以前的日期。

回答by sohaiby

You have to set the DisplayDateStartattribute with Today's date

您必须DisplayDateStart使用今天的日期设置属性

<DatePicker Name="dt_StartDateFrom" DisplayDateStart="{x:Static sys:DateTime.Today}">
</DatePicker>

Make sure you have set the

确保您已设置

xmlns:sys="clr-namespace:System;assembly=mscorlib"

xmlns:sys="clr-namespace:System;assembly=mscorlib"

in your <UserControl>tag to be able to use the sys:parameter

在您的<UserControl>标签中,以便能够使用sys:参数

P.S. To Disable future dates, you can use DisplayDateEndattribute

PS要禁用未来日期,您可以使用DisplayDateEnd属性

回答by keyboardP

You can set the DisplayDateStartproperty to today.

您可以将DisplayDateStart属性设置为今天。

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);

回答by Subhash Jakhar

You can disable the past days in calender in this way : -

您可以通过这种方式在日历中禁用过去的日子:-

Caldr.DisplayDateStart = DateTime.Today;

Caldr.DisplayDateStart = DateTime.Today;

回答by Mjollnir

To block out the past dates (everything before today), you can simply use the Framework's 4.0+ method : AddDatesInPast()

要屏蔽过去的日期(今天之前的所有内容),您只需使用框架的 4.0+ 方法:AddDatesInPast()

myCalendar.BlackoutDates.AddDatesInPast();

It does the same as the accepted answer but is more straightforward.

它与接受的答案相同,但更直接。

回答by Neeraj

Thisis a neater solution to the problem in case we want to add it through xaml.

如果我们想通过 xaml 添加它,是一个更简洁的问题解决方案。

回答by user3564895

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.

在 WPF 中,您想使用 MVVM 绑定到您的视图模型。这种方法更通用,可让您在单元测试中进行边界检查。

   <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.

设置 MinAvailDate = DateTime.Now,或调整您的时区的默认值。
以类似的方式实现 MaxAvailDate,以及绑定到 SelectedDate。

回答by Rameshbabu

I like a @sohailby answer but I just want to change little by using blackoutdates

我喜欢@sohailby 的答案,但我只想通过使用 blackoutdates 来改变一点


  <DatePicker.BlackoutDates>
     <CalendarDateRange End="{x:Static sys:DateTime.Today}" Start="{x:Static sys:DateTime.MinValue}" />
  </DatePicker.BlackoutDates>