Obtaining value from Calendar control without selecting a day

892 views Asked by At

Is there a way (using a Calendar control in Visual Studio WPF) I can obtain a value without actually selecting a day within the control.

For example, I have a client that wants the calendar's DisplayMode = Year. And now let's say they select the month of March within the control. That will change the DisplayMode to Month and no value is EVER set until they select a day in that month within the control.

Is there a way I can obtain the year and month and set it to a value without them actually selecting a day? Because in this case the day does not matter.

I've found some workarounds that don't involve using the Calendar control or DatePicker but I would like to continue using these controls for UI purposes.

1

There are 1 answers

4
SteveFerg On BEST ANSWER

You will need to create a DisplayModeChanged event and change the mode accordingly:

private DateTime newdate = DateTime.Now;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   calctl.SelectedDate = DateTime.Now;
   calctl.DisplayMode = System.Windows.Controls.CalendarMode.Year;
}
private void calctl_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
{
        if (calctl.DisplayMode == System.Windows.Controls.CalendarMode.Month)
        {
             newdate = calctl.DisplayDate;
             calctl.DisplayMode = System.Windows.Controls.CalendarMode.Year;
        }
}

This will allow you to change the month and/or year and not bother with the day. You will want to set the initial display mode to month.