Silverlight DatePicker仅限YearMonth和Year

时间:2014-04-25 15:43:11

标签: c# silverlight datepicker silverlight-toolkit

我需要一个Silverlight DatePicker控件,允许用户选择Year或选择YearMonth。允许我在xaml中设置这种类型的功能:

<sdk:DatePicker my:DatePickerCalendar.IsYear="True" my:DatePickerFormat="yyyy"/>

OR

<sdk:DatePicker my:DatePickerCalendar.IsMonthYear="True" my:DatePickerFormat="MM/yyyy"/>

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此修改版本的silverlight日期选择器控件来仅显示月份或年份

<sdk:DatePicker Name="datepicker1"
                Height="23"
                Width="150"
                SelectedDateFormat="MonthYear"
                CalendarMode="Year" />

DatePicker Month or year

答案 1 :(得分:1)

我找到的最简单的解决方案是

的组合

DateTimePicker start on year

private void dp_CalendarOpened(object sender, RoutedEventArgs e)
    {
        DatePicker dp = (DatePicker)sender;
        Popup popup = (Popup)dp.Template.FindName("PART_Popup", dp);
        Calendar cal = (Calendar)popup.Child;

        cal.DisplayMode = CalendarMode.Year;
        cal.DisplayModeChanged += cal_DisplayModeChanged;
    }

private void cal_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
    {
        Calendar calendar = (Calendar)sender;
        Popup popup = (Popup)calendar.Parent;

        if (e.NewMode == CalendarMode.Month)
        {
            //calendar.DisplayMode = e.OldMode;
            calendar.SelectedDate = calendar.DisplayDate.AddDays((calendar.DisplayDate.Day * -1) + 1).AddMonths(1).AddDays(-1);
            popup.IsOpen = false;

            calendar.DisplayModeChanged -= cal_DisplayModeChanged;
        }
    }