Recent projects, query only need a year and month, do not need the day, so need to modify the original DatePicker, query the content of the Internet, and finally from a post see the method of adding additional properties, the address is http://stackoverflow.com/ Questions/1798513/wpf-toolkit-datepicker-month-year-only
The original is used in two classes, one of which is to let DatePicker under the calendar only display years, do not display the day, another class is to let the DatePicker format yyyy-mm format, but from the article can be seen, someone proposed, with the format of the class to format, The DatePicker control will blink, of course not affect the use. If you do not want to use the class mentioned in the article to format, you can also use the method in my previous article to format, there will be no flashing situation.
Classes that show only the month and year
Public classDatepickercalendar { Public Static ReadOnlyDependencyProperty Ismonthyearproperty =dependencyproperty.registerattached ("Ismonthyear",typeof(BOOL),typeof(Datepickercalendar),NewPropertyMetadata (onismonthyearchanged)); Public Static BOOLgetismonthyear (DependencyObject dobj) {return(BOOL) dobj. GetValue (Ismonthyearproperty); } Public Static voidSetismonthyear (DependencyObject dobj,BOOLvalue) {dobj. SetValue (ismonthyearproperty, value); } Private Static voidonismonthyearchanged (DependencyObject dobj, DependencyPropertyChangedEventArgs e) {varDatePicker =(DatePicker) dobj; Application.Current.Dispatcher. BeginInvoke (dispatcherpriority.loaded,NewAction<datepicker, dependencypropertychangedeventargs>(setcalendareventhandlers), DatePicker, E); } Private Static voidsetcalendareventhandlers (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 voidDatepickeroncalendaropened (Objectsender, RoutedEventArgs RoutedEventArgs) { varCalendar =Getdatepickercalendar (sender); Calendar. DisplayMode=calendarmode.year; Calendar. Displaymodechanged+=calendarondisplaymodechanged; } Private Static voidDatepickeroncalendarclosed (Objectsender, RoutedEventArgs RoutedEventArgs) { varDatePicker =(DatePicker) sender; varCalendar =Getdatepickercalendar (sender); Datepicker.selecteddate=Calendar. SelectedDate; Calendar. Displaymodechanged-=calendarondisplaymodechanged; } Private Static voidCalendarondisplaymodechanged (Objectsender, Calendarmodechangedeventargs e) { varCalendar =(Calendar) sender; if(Calendar. DisplayMode! =calendarmode.month)return; Calendar. SelectedDate=getselectedcalendardate (Calendar. Displaydate); varDatePicker =Getcalendarsdatepicker (Calendar); Datepicker.isdropdownopen=false; } Private StaticCalendar Getdatepickercalendar (Objectsender) { varDatePicker =(DatePicker) sender; varPopup = (popup) datePicker.Template.FindName ("Part_popup", DatePicker); return(Calendar) popup. Child); } Private StaticDatePicker Getcalendarsdatepicker (FrameworkElement child) {varParent =(FrameworkElement) child. Parent; if(parent.) Name = ="Part_root") return(DatePicker) parent. TemplatedParent; returnGetcalendarsdatepicker (parent); } Private StaticDatetime? Getselectedcalendardate (DateTime?)selectedDate) { if(!selecteddate.hasvalue)return NULL; return NewDateTime (SelectedDate.Value.Year, SelectedDate.Value.Month,1); } }
Datepickercalendar
Invocation mode
<datepicker width= "" "Height= " local:d atepickercalendar.ismonthyear="True"/>
Show effect
From the figure can see different, the left side is to add additional properties, click to display the month directly, after the side of useless is displayed to the day, although all formatted in order to yyyy-mm, but the calendar if displayed to the day, the user experience is not good.
Go WPF datepicker only displays the year and month