<Calendar SelectionMode="SingleRange"
Name="MonthlyCalendar"
SelectedDatesChanged="MonthlyCalendar_SelectedDatesChanged"
DisplayDateChanged="MonthlyCalendar_DisplayDateChanged"
DisplayModeChanged="MonthlyCalendar_DisplayModeChanged"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,10,0,0">
</Calendar>
The code behind for these events looks like Listing 4.
private void MonthlyCalendar_SelectedDatesChanged(object sender,
SelectionChangedEventArgs e)
{
}
private void MonthlyCalendar_DisplayDateChanged(object sender,
CalendarDateChangedEventArgs e)
{
}
private void MonthlyCalendar_DisplayModeChanged(object sender,
CalendarModeChangedEventArgs e)
{
}
Calendar Events
Besides the normal control events, the Calendar control has three events calendar related events. These events are the DisplayDateChanged, DisplayModeChanged, and SelectedDatesChanged. The DisplayDateChanged event is fired where the DisplayDate property is changed. The DisplayModeChanged event is fired when the DisplayMode property is changed. The SelectedDatesChanged event is fired when the SelectedDate or SelectedDates properties are changed. The following code snippet sets these three events attributes.
Listing 4
Normally, on a date selection, you may want to capture that event and know what the current selected date is. Now how about we add a TextBox control to the page and on the date selection, we are going to set the text of the TextBox to the currently selected date.
We add the following code to the XAML just below the Calendar control.
<TextBox Width="200" Height="30"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
Margin="10,10,10,10"
x:Name="SelectedDateTextBox">
</TextBox>
On the SelectedDateChanged event handler, we set the TextBox.Text property to the SelectedDate property of the Calendar control as you can see from code in Listing 5.
private void MonthlyCalendar_SelectedDatesChanged(object sender,
SelectionChangedEventArgs e)
{
SelectedDateTextBox.Text = MonthlyCalendar.SelectedDate.ToString();
}
Listing 5
Now when you run the application, you will see output looks like Figure 10. When you select a date in the Calendar, it will be displayed in the Textbox.
Figure 10
http://www.c-sharpcorner.com/uploadfile/mahesh/wpf-calendar-control/