[Switch] WPF DatePicker only displays year and month, wpfdatepicker
For the latest project, only the year and month are required for query, and no day is required. Therefore, you need to modify the original DatePicker and query the online content, finally, I saw from a post that the method for adding additional properties, the address is http://stackoverflow.com/questions/1798513/wpf-toolkit-datepicker-month-year-only
Two classes are used in the original article. One is to enable the Calendar under DatePicker to display only the year, not the day, and the other is to format the DatePicker to the yyyy-MM format, however, it can be seen from the article that some people have proposed that when formatting with the formatting class, the DatePicker control will flash, of course, without affecting the use. If you do not want to use the class mentioned in the article for formatting, you can also use the method in the previous article for formatting without flashing.
Show only the year and month classes
Public class DatePickerCalendar {public static readonly DependencyProperty IsMonthYearProperty = DependencyProperty. registerAttached ("IsMonthYear", typeof (bool), typeof (DatePickerCalendar), new PropertyMetadata (metadata); public static bool GetIsMonthYear (DependencyObject dobj) {return (bool) dobj. getValue (IsMonthYearProperty);} public static void SetIsMonthYear (DependencyObject dobj, Bool value) {dobj. setValue (IsMonthYearProperty, value);} private static void OnIsMonthYearChanged (DependencyObject dobj, DependencyPropertyChangedEventArgs e) {var datePicker = (DatePicker) dobj; Application. current. dispatcher. beginInvoke (DispatcherPriority. loaded, new Action <DatePicker, DependencyPropertyChangedEventArgs> (SetCalendarEventHandlers), datePicker, e);} private static void SetCa LendarEventHandlers (DatePicker datePicker, DependencyPropertyChangedEventArgs e) {if (e. newValue = e. oldValue) return; if (bool) e. newValue) {datePicker. calendarOpened + = DatePickerOnCalendarOpened; datePicker. calendarClosed + = DatePickerOnCalendarClosed;} else {datePicker. calendarOpened-= DatePickerOnCalendarOpened; datePicker. calendarClosed-= DatePickerOnCalendarClosed;} private static Void DatePickerOnCalendarOpened (object sender, RoutedEventArgs routedEventArgs) {var calendar = GetDatePickerCalendar (sender); calendar. displayMode = CalendarMode. year; calendar. displayModeChanged + = CalendarOnDisplayModeChanged;} private static void upload (object sender, RoutedEventArgs routedEventArgs) {var datePicker = (DatePicker) sender; var calendar = GetDatePickerCale Ndar (sender); datePicker. selectedDate = calendar. selectedDate; calendar. displayModeChanged-= CalendarOnDisplayModeChanged;} private static void CalendarOnDisplayModeChanged (object sender, CalendarModeChangedEventArgs e) {var calendar = (Calendar) sender; if (calendar. displayMode! = CalendarMode. month) return; calendar. selectedDate = GetSelectedCalendarDate (calendar. displayDate); var datePicker = GetCalendarsDatePicker (calendar); datePicker. isDropDownOpen = false;} private static Calendar GetDatePickerCalendar (object sender) {var datePicker = (DatePicker) sender; var popup = (Popup) datePicker. template. findName ("PART_Popup", datePicker); return (Calendar) popup. child);} pri Vate static DatePicker GetCalendarsDatePicker (FrameworkElement child) {var parent = (FrameworkElement) child. parent; if (parent. name = "PART_Root") return (DatePicker) parent. templatedParent; return GetCalendarsDatePicker (parent);} private static DateTime? GetSelectedCalendarDate (DateTime? SelectedDate) {if (! SelectedDate. HasValue) return null; return new DateTime (selectedDate. Value. Year, selectedDate. Value. Month, 1 );}}DatePickerCalendar
Call Method
<DatePicker Width="200" Height="30" local:DatePickerCalendar.IsMonthYear="True"/>
Display Effect
From the figure, we can see the difference between the left and right. The left side is added with additional attributes, and the month is displayed directly after you click it. If it is useless on the back side, it is displayed to the day, although it is formatted as yyyy-MM, however, if the Calendar is displayed to the day, the user experience is poor.