【问题标题】:how to check whether selected dates contains from calendar contains date less than todays date如何检查所选日期是否包含日历中包含的日期小于今天的日期
【发布时间】:2016-01-19 06:31:39
【问题描述】:

我需要检查从日历控件中选择的日期(多个)是否包含任何已经过去或小于今天的日期。 在 c# wpf 应用程序中怎么可能。

【问题讨论】:

  • mvvm环境还是简单环境?
  • 您能否展示您的代码,说明您如何存储日历控件中选定的日期?
  • 我正在使用鼠标在 UI 中同时从日历中选择多个日期。所以我需要获取所有日期并将每个日期与今天的日期进行比较。
  • 我使用的是简单环境

标签: c# wpf date calendar


【解决方案1】:

试试下面的代码

SelectedDatesCollection selectedDatesCollection = myCalendar.SelectedDates;

if (selectedDatesCollection.Count > 0)
{
    if (selectedDatesCollection.Any(x => x < new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)))
        MessageBox.Show("passed or less than today");
    else
        MessageBox.Show("today or future date");
}

【讨论】:

  • 哇...太棒了。简简单单。
【解决方案2】:
private void CreateDynamicCalendar()  {  
Calendar MonthlyCalendar = new Calendar();  
MonthlyCalendar.Name = "MonthlyCalendar";  
MonthlyCalendar.Width = 300;  
MonthlyCalendar.Height = 400;  
MonthlyCalendar.Background = Brushes.LightBlue;  
MonthlyCalendar.DisplayMode = CalendarMode.Month;  
MonthlyCalendar.SelectionMode = CalendarSelectionMode.MultipleRange;  
MonthlyCalendar.DisplayDateStart = new DateTime(2010, 3, 1);  
MonthlyCalendar.DisplayDateEnd = new DateTime(2010, 3, 31);  
MonthlyCalendar.SelectedDates.Add(new DateTime(2010, 3, 5));  
MonthlyCalendar.SelectedDates.Add(new DateTime(2010, 3, 15));  
MonthlyCalendar.SelectedDates.Add(new DateTime(2010, 3, 25));  /*You Can Check all selected dates here with respect to current date*/

MonthlyCalendar.FirstDayOfWeek = DayOfWeek.Monday;  
MonthlyCalendar.IsTodayHighlighted = true;  

LayoutRoot.Children.Add(MonthlyCalendar);  }   

您可以点击此链接查看 WPF 日历教程

http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-calendar-control/

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2016-05-20
    • 2011-09-26
    相关资源
    最近更新 更多