"Go" WPF DatePicker show only year and month

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.