[WPF] Chapter XXV date control study

Original: [WPF] Chapter XXV date control study

  WPF controls included two dates: Calender and DatePicker. Both controls are designed to allow the user to select a date.

  Calendar Control displays the date, similar to the calendar (for example, when configuring the system date to see the calendar) to see with the Windows operating system. The control displays one month, allowing the jump from one month to another month (by clicking the arrow button), or jump to a specific month (see the months of the year title by clicking the first month, and then click month).

  DatePicker control requires less space. It is a simple model of the text box, the text box to save the date or date format string length short date format. DatePicker control provides a drop-down arrow that, when clicked, will pop up a full calendar attempt, the attempt to try to Fairchild Calendar control and display. This attempt to display pop-up on top of any other content, like a drop-down combo box.

  The following figure shows two display modes supported by the Calendar control, and DatePicker supports two date formats.

 

   Calendar and DatePicker controls provided property allows you to determine what date and (in a continuous range to provide these dates) to choose the date upon which the display. The following table lists the available attributes:

Table DatePicker class attributes and Calendar

 

   Date Controls also provides several different events. The most useful event is SelectedDateChanged event DatePicker control of, or the Calendar control similar SelectedDatesChanged event, which adds support for multiple date selection. You may refuse to respond to these events in order to select a specific date, such as the weekend Date:

复制代码
 private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (DateTime selectedDate in e.AddedItems)
            {
                if ((selectedDate.DayOfWeek == DayOfWeek.Saturday) ||
                    (selectedDate.DayOfWeek == DayOfWeek.Sunday))
                {
                    lblError.Text = "Weekends are not allowed";

                    ((Calendar)sender).SelectedDates.Remove(selectedDate);
                }
            }
        }
复制代码

  可使用支持单个或多个日期选择的Calendar事件加以测试。如果支持多个选择,那么尝试在整个星期的日期上拖动鼠标。除不允许的周末日期外,其他所有日期将保持突出显示,而周末日期将被自动取消选择。

  Calendar控件还添加了DisplayDateChanged事件(当用户浏览到新的月份时触发该事件)。DatePicker控件添加了CalendarOpened和CalendarClosed事件(当下拉日历显示和关闭时触发这两个事件),以及DateValidationError事件(当用户在文本输入部分输入不能被解释为合法时间的值时触发该事件)。通常,但用户打开日历试图时会丢弃非法值,但可以选择填充一些文本以通知发生了问题:

private void DatePicker_DateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
        {
            lblError.Text = "'" + e.Text +
                "' is not a valid value because " + e.Exception.Message;
        }

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12285976.html