Question

I need a Silverlight DatePicker Control that allows a user to select a Year only or to select YearMonth. allowing me to set this type of functionality in xaml something like this:

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

OR

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

Solution

Easiest solution I found was a combination of

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;
        }
    }

OTHER TIPS

You may try this Modified version of silverlight Date Picker control to display only Month or Year

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

DatePicker Month or year

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top