DateTimePicker start on year

一个人想着一个人 提交于 2019-12-12 05:26:39

问题


There are several QAs here about formatting C# DateTimePickers, but none seem to address this aspect:

The default behavior is when the user clicks the DateTimePicker dropdown he starts at the one-month view, with a scroll bar on top to page through one month at a time.

From the UI, you can move up to year level, but my operators are not local or intuitive with control use. I'm requesting that the user enter data that spans 10 years (date of manufacture for some product). I want to start at the year level to make it faster/easier to start near the right arbitrary place.

Can I make the calendar pulldown automatically start at year level and drill down?

Thanks.


回答1:


If you are using WPF then I presume you are referring to the DatePicker control.

In your XAML:

<DatePicker CalendarOpened="DatePicker_CalendarOpened" />

and then in the code behind

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

private void DatePicker_CalendarOpened(object sender, RoutedEventArgs e)
{
    DatePicker datepicker = (DatePicker)sender;
    Popup popup = (Popup)datepicker.Template.FindName("PART_Popup", datepicker);
    Calendar cal = (Calendar)popup.Child;
    cal.DisplayMode = CalendarMode.Year;
}


来源:https://stackoverflow.com/questions/14983649/datetimepicker-start-on-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!